PHP Lesson 3: Variables and Constants

Variables

Variables are containers, they contain data. PHP is a loosely typed language, and thus the variable can contain any type of data, PHP will change the type of the variable implicitly, depending on the operation being performed on it. This is in strong contrast with strongly typed languages, such as Java, or C++, where a variable can only hold one data type troughout its whole life.
In PHP, variables are identified by the dollar sign: $, followed by the name you wish to give to your variable. These names can contain bot upper- and lowercase letters, numbers, and the underscore character. Other characters are not allowed in variablenames. The name can start with any letter, or an underscore, but never with a number. So, $varname, $Varname and $_varname are all correct, $1varname is not correct.

Variable variables
PHP offers the extremely powerful possibility to create what is called a variable variable. This is a variable whose name is contained in another variable. An example:

$varname = ‘Tom’;
/* Tom is the name of the variable */
$$varname = ‘Tim’;
/* Tim is the value you assign to the variable named Tom. You do this through the variable $varname, which holds the the variablename Tom */
echo $Tom;
/* This will display ‘Tim’ */



The same works with numbers! This means you can circumvent the variable naming restrictions mentioned above. You can also do this by defining the name between braces: ${123};
And, even better, there is also something similar to variable variables, but with functions. This means you can put a function name inside a variable, and execute the function by calling the variable.

function anyFunction(){
echo ‘icantinternet.org rules!’;
}

$anyVar = ‘anyFunction’;
$anyVar();  //This will call and execute the function anyFunction, and echo the truth!

I suppose it holds no surprise that all this is extremely powerful, and must be used with extreme caution to avoid programming mistakes, and possibly hacking risks.

photo-2015-08-02-08-43-22_3130
Does a variable exist?
This is a question that is not always easy to answer, due to the way PHP handles its varuables. This can lead to annoying warnings when running a scripts, up to security and functionality issues. Luckily, PHP also has a special construct to check if a variable exists and has a value set (this means, other than NULL): isset()
echo isset ($astupidvariable);

Calling isset() will return a true to you if a variable exists and has a value (other than NULL), and will return false to you when your variable doesn’t exist yet, or doesn’t have a value yet.

Constants

On the other side of the ring are the constants, which are, conversely to variables, meant for constant values (I know, it was a blinding flash of the obvious). The visibility of a constant is throughout the whole script, but, unlike variables, they can only hold scalar data, no compound data.
Constants follow the same naming conventions as variables do, so upper- and lowercase letters, numbers, and underscore, but not starting with numbers. Also, constant names do not have a leading dollar sign $. Best practice is to define your constants in all uppercase. This will give you clearer and better understandable (and thus maintainable) code.

define (‘GREATEST_SITE’, ‘beheydt.be’); //valid name, now, throughout the code, it is known that beheydt.be is in the constant GREATEST_SITE
echo GREATEST_SITE; //Displays the truth again (beheydt.be for those who didn’t get it 😉 ).

Leave a Comment





For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

If you agree to these terms, please click here.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This

Share This

Share this post with your friends!

%d bloggers like this: