Studyon Minte9.com
ZCE 5.3

Study

Variables



	// Php variables are loosely typed; case sensitive
	// Name can not start with a number
	// Test variable if exists with !empty
	// isset($a,$b); emtpy(trim($a)); // Fatal error


* Variables

PHP is loosely typed, meaning that it will implicitly change the type of a variable as needed, 
depending on the operation being performed on its value. 
This contrasts with strongly typed languages, like C and Java, where variables can only contain one type of 
data throughout their existence.

Must be named using only letters, numbers and underscore.
Must start with a letter or an underscore. 

<php $name = 'valid'; // Valid name $_name = 'valid'; // Valid name $1name = 'invalid'; // Invalid name, starts with a number
  Are one of only two identifier types in PHP that are case-sensitive (the other is constants)
<?php error_reporting(E_ALL); $Name = "John"; echo $name; // Notice: Undefined variable: name
  * Variable Variables
<?php $name = 'foo'; $$name = 'bar'; echo $foo; // Displays 'bar'
  It is possible to create variables whose names do not follow the constraints listed above
<?php $name = '123'; $$name = '456'; echo ${'123'}; // output 456
  Variable variables are a very powerful tool, and should be used with extreme care. Their improper use can lead to some significant security issues. A technique similar to variable variables can also be used to hold function names inside a variable:
<?php function myFunc() { echo 'myFunc!'; } $f = 'myFunc'; $f(); // output myFunc!
  * Variable exists One of the downsides of the way PHP handles variables is that there is no way to ensure that any one of them will exist at any given point in the execution of a script. A call to isset() will return TRUE if a variable exists and has a value other than NULL A call to empty() will returns TRUE if var has a empty and zero value ("", 0, NULL)
<?php if(isset($_POST['myField']) && $_POST[myField] != "") ... code if(!empty($_POST['myField'])) // a more efficient way ... code
 
<?php error_reporting(E_ALL); if ($a) echo 'Yes'; // Notice: Undefined variable: a if (!empty($a)) echo 'Yes'; // Output: nothing (better)
  isset() - Determine if a variable is set and is not NULL
<?php $a = 1; $b = 0; $c = ""; $d = null; echo isset($a); // TRUE echo isset($a, $b); // TRUE echo isset($a, $b, $c); // TRUE echo isset($a, $b, $c, $d); // FALSE
  empty() - Determine whether a variable is empty
<?php // Determine whether a variable is considered to be empty $a = ""; echo empty($a); // TRUE $a = 0; echo empty($a); // TRUE $a = "0"; echo empty($a); // TRUE $a = NULL; echo empty($a); // TRUE $a = FALSE; echo empty($a); // TRUE $a = array(); echo empty($a); // TRUE // You must check only variables, as anything else will result in a parse error $a = 1; empty(trim($a)); // Fatal error