PHP Variables and Variable Functions

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
PHP Variables

In PHP, variable names are case-sensitive. These variables: $test, $Test, $TEST, $tesT all are different variables. Unlike C languages, you do not have to declare a variable before you use it. If you use a variable that you have not created in a script, then PHP will just create a new variable for you. This may make it harder to find your error.

Creating a variable

PHP variables start with a dollar sign. The first character must be a letter. Other than that, you can use any letter or number. The variable name can be as long and as short as you want.

Examples:

Code:
$text = "test";
$text = 5;

Another thing to note, is that you can change the type of variable. This differs from Java and C++. This can also cause a source of error if you are not careful. First the $text variable was a string variable, and then it becomes an integer variable.

Constants

A constant variable is a variable that once defined, can never be changed. I like to put all my constants in strict upper case. This makes it easier to find. A useful example of constants is database access information.

Once this information is set, you wouldn't want to change it.

We create constants using the define function.

Examples:

Code:
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","pass");

The first parameter is the name of the constant. The second parameter is the value of the parameter. So I could connect to a database like this:

Code:
mysql_connect(DB_HOST,DB_USER,DB_PASS);

You can use them just like any other variable. The only thing is you can't change them.

If I try to change the variable, I get an error.

Example:

Code:
DB_HOST = "Test";

The error I get is:



This is more than useless because I don't have errors set very well in my php.ini file. All I know is that I have an error on line 54. This is referring to where I try to change the value of the constant.

The third optional parameter allows me to specify if the constant is case sensitive or not. By default DB_HOST and db_host are the same constants. If I pass false as the third parameter, this makes DB_HOST and db_host both two different constants.

Example:

Code:
define("DB_HOST2","localhost",false);

echo DB_HOST2 . " " . db_host2

When I run this code, I get this warning:



You don't have to pass true. The function assumes a default value of true, if you don't pass a third argument.

Checking Variable Type

PHP provides method is_int, is_string, is_array and so on for you to assess what type of data a variable is holding currently. Note that at one point in the string is_int can return true, and return false later. Variable types can change.

Example:

Code:
$x = "test";
$y = 5.5;
$b = false;
$test = 5;

if (is_int($x)) {
	echo "$$x is an int.<br />\n";	
} else {
	echo "$$x is a " . gettype($x) . "<br />\n";	
}

This code tests the what type of data $x is holding. I use two $ signs as to avoid printing the content of the variable. The gettype method returns the type of the data.

So this code outputs:



So if tested $x with is_string, the method is_string would return true.


Isset, unset and empty

The isset method returns true if the variable has been set. This is similar to the empty function but they are not the same. The empty function returns true if a variable has not been assigned a value or has a false value. PHP considers "" (empty string), false, and 0 to be empty. Even though the variable has been assigned a value.

Example:

Code:
$x = 0;
	
if (empty($x)) {
	echo "Empty.";	
} else {
	echo "Not empty.";	
}

Output:



However, even with a false value, isset will return true. This function simply checks if the variable has a value.

Example:

Code:
$x = 0;
	
if (isset($x)) {
	echo "Set.";	
} else {
	echo "Not set.";	
}

Output:



The unset function removes a variable, so that isset will return false.

Example:

Code:
unset($x);
if (isset($x)) {
	echo "Set.";	
} else {
	echo "Not set.";	
}

Output:

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top