Learning PHP


Posted 5 years ago by Ryan Dhungel


Category: Web Development PHP



Introduction


Do you want learn and understand PHP programming from the root and be able to build amazing web applications? Then you are in the right place.

While trying to learn any Programming language, you should begin with data types.


Prerequisite

  • Absolute basic knowledge of PHP
  • Local development envintoment setup to start writting PHP programs

Lessons 4



PHP provides eight data types. Data types are basically type of values that you can use in your program.

Scalar (Single value) types

  • Integers (whole numbers such as 1, 10, 99)
  • Floating point numbers (numbers with decimal digits such as 1.24, 0.05)
  • Strings (sequence of characters such as 'hello world', 'Hi')
  • Booleans (true, false)

Collection types

  • Objects
  • Arrays

Special types

  • Resource
  • NULL

 



Understanding PHP Arrays is the key to understand and ultimately master PHP. So lets understand arrays better:

  • An array holds a group of values
  • You can identify these values by position
  • Position could be number based of string based
  • Numeric position will begin with 0 being the first position, not 1
  • Arrays with identifying string is called associative array

Array (Numeric index)

$person[0] = "Ryan";
$person[1] = "Zen";
$person[2] = "Nik";

 

Array (Associative index)

$creator['Light'] = "Edison";
$creator['Rotary Engine'] = "Wankel";
$creator['Aeroplane'] = "Right Brothers";

 

Creating Array

To create an array, use array() construct:

$person = array("Ryan", "Zen", "Nik");
$creator = array('Light' => "Edison", 'Rotary Engine' => "Wankel", 'Aeroplane' => "Right Brothers");

 

Loop through arrays

// simple way
foreach ($person as $name) {
	echo "Hello, {$name} <br>";
}

// using key value
foreach ($creator as $invention => $inventor) {
	echo "{$inventor} created the {$invention} <br>";
}

 

Sorting an array

sort($person);
sort($creator);

 

Determining if it's an array

if (is_array($person)) {
	echo '$person is an array <br>';
}

This is a basic overview of PHP arrays. We will be going through Arrays in details in future.



PHP supports OOP (Object Oriented Programming). OOP helps you write clean and maintainable code. Classes are the building blocks of OOP. A class is a defination of a structure that contains properties(variables) and methods(functions).

 

Creating a new class(object)

class Person {
	public $name = '';

	function name($newname = NULL) {
		if (!is_null($newname)) {
			$this->name = $newname;
		}
		return $this->name;
	}
}

 

  • Once class is defined, Any number of objects can be made from it with new keyword. 
  • The Object's properties and methods can be accessed with -> construct:

 

Instantiating a new class

$p = new Person();
$p->name('Ryan');
echo "Hello {$p->name} <br>"; // Hello Ryan 

$p1 = new Person();
$p1->name('Zen');
echo "Hello {$p1->name} <br>"; // Hello Zen

 

Determine if it's Object

if (is_object($p)) {
	echo '$P is an object';
}

 

This is just a very basic overview of Objects. You will learn more about Objects in future lessons.



In PHP, variables are prefixed with dollar sign ($).

$name
$Age

 

A variable may hold any type of value

$name = "Ryan";
$name = 30;
$name = array("Ryan", 30, " Zen");

 

Variable Variables

You can reference the value of a variable whose name is stored in another variable using $$.

$name = "ryan";
$$name = "zen"; // when using $$, you are referencing the 'value' of a variable not the variable itself
echo $ryan; // zen

Variable References

You can create variable aliases using references. To make $one an alias for the variable $two, use:

$one =& $two; // the old value of $one, if any, is lost and replaced by the value of $two.

 

Two different variables are altername names for the same value

$bigLongVariable = "PHP";
$short = &$bigLongVariable;

$bigLongVariable .= " rocks!";

print '$short is ' . "$short <br>"; // $short is PHP rocks! 
print "Long is $bigLongVariable"; // Long is PHP rocks!

 

Variable Scope

There are four types of variable scopes in PHP.

  • Local
  • Global
  • Static
  • Function parameters

 

Local Scope

A variable declared in a function is visible only to code in that function. It is not accessible outside the function.

function person() {
	// variables with local scope
	$name = "Ryan";
	$age = 30;
	echo $age;
}

person(); // 30
echo $name; // undefined variable

 

Global Scope

Variables declared outside a function are global. They can be accessed by any part of the program. They are not available inside functions. To allow a function to access a global variable, you can use the global keyword.

$counter = 10;
function updateCounter() {
	global $counter;
	$counter++;
}

updateCounter(); // execute the function that increments counter
echo $counter; // 11

 

Accessing global variable using PHP's $GLOBALS array

 

$counter = 10;
function updateCounter() {
	$GLOBALS[counter]++;
}

updateCounter(); // execute the function that increments counter
echo $counter; // 11

 

Static variables

Static variable retains its value between different calls to that function. It is visible only within that function.

$counter = 10;
function updateCounter() {
	static $counter = 0;
	$counter++;

	echo "Static counter is now {$counter} <br>";
}

updateCounter(); // Static counter is now 1
updateCounter(); // Static counter is now 12
echo "Global counter is {$counter}"; // Global counter is 10

 

Function parameters

A function definition can have named parameters:

function greet($name) {
	echo "Hello, $name";
}
greet("Zen"); // Hello Zen

 

isset unset variable

$p1 = isset($name);
var_dump($p1); // false

$name = "Ryan";
$p2 = isset($name);
var_dump($p2); // true