Die2mrw007 Posted December 25, 2011 Report Posted December 25, 2011 The names of PHP variables must follow a certain number of rules: PHP variables must start with the symbol $, followed by a letter or by an underscore. PHP variables can contain alphanumeric symbols (A-Z, a-z, 0-9) and underscores (_) only (they cannot contain spaces). PHP variables are case sensitive, meaning that the variable $hello will be considered as different from the variable $Hello. Learn the PHP code: <?php $hello = 0; $Hello = 1; echo $hello; echo $Hello; ?> Run the PHP script in your web browser: Remark: A variable is assigned by using the symbol =. PHP VARIABLES - TYPES OF VARIABLES IN PHP The different PHP types PHP variables can have different types; among those, we can distinguish: booleans, that is to say one of the two values TRUE or FALSE integers, which are elements of the set Z = {...,-2,-1,0,1,2,...} floating point numbers (also called "doubles" or "real numbers"), which represent numbers with decimals. strings, which are series of characters put between quotes; for instance, 'a black cat' is a string. arrays, which are tables indexed by keys (we will give an introduction to arrays in another PHP tutorial) objects, which are user-defined. resources, which refer to external resources. Resource variables are created by special PHP functions. the NULL value, which denotes the fact that a variable has no value. A variable has NULL value if it has been assigned the NULL value, if it hasn't yet been set, or if it has been unset. You can test whether a PHP variable has NULL value by using the function is_null() (which will return TRUE if the tested PHP variable is NULL and FALSE otherwise). pseudo-types, which are hybrid PHP types. For instance, mixed indicates that a parameter can be of various types (the function gettype() takes mixed parameters); another example is the pseudo-type number, which denotes an integer or a floating point number. Absence of type definition in PHP When declaring a variable in PHP, you do not need to define its type; indeed, PHP is very weakly typed and the type of a variable will be automatically determined by the type of content that was assigned to it. You can have a look at the example below for a demonstration of this fact: Learn the PHP code: <html> <body> <?php $var = 2; echo $var.' '.gettype($var).'<br>'; $var='GizmoLord Forum'; echo $var.' '.gettype($var); ?> </body> </html> Remark: the point . is the concatenation operator (it allows you to "stick together" different strings; since PHP is a very weakly typed language, the $var variable in the example above is interpreted as a string when the concatenation operator is used). Run the PHP script in your web browser: Functions related to the types of variables in PHP Several functions allow you to manipulate the type of a variable in PHP; we enumerate below a few of them (the list is in no way exhaustive): isset() returns TRUE if the PHP variable it receives has been set and FALSE otherwise. unset() allows you to "empty" a variable (i.e. to reinitialize it, or in other words to "free" it; in particular, the variable which is unset will lose its type and will be assigned the NULL value). gettype() returns the type of the PHP variable it receives (cf. example above). is_string() returns TRUE if the variable received is a string, FALSE otherwise. By the same token, you can use the functions is_integer(), is_float(), is_array(), etc ... PHP VARIABLES - ASSIGNMENT OF VARIABLES IN PHP There are two ways to assign a variable in PHP. Assignment by value With the assignment by value (which is the most often used way to define a variable), a variable is assigned a value only. For instance, setting $var2 = $var1 will assign the value of $var1 to the new variable $var2; if later on in the script the variable $var1 is changed, the variable $var2 will remain unchanged ($var2 will keep the value at which it was first defined). The example below is illustrating the assignment of PHP variables by value. Learn the HTML code: <html> <body> <?php $var1 = 1; $var2 = $var1; echo $var2.' '; $var1 = 0; echo $var2; ?> </body> </html> Run the PHP script in your web browser: Assignment by reference: After you assign a variable by reference, the variable points to a location in memory instead of merely storing a given value as in an assignment by value. A sa result, the value of this variable will be modified whenever the value stored at this memory address is changed. When you are assigning a variable $var1 by reference to another variable $var2, the only difference in the PHP syntax is that you must add an ampersand & before $var1: Learn the PHP code: <html> <body> <?php $var1 = 1; $var2 = ~$var1; echo $var2.'<br>'; $var1 = 0; echo $var2; ?> </body> </html> Run the PHP script in your browser: PHP VARIABLES - VARIABLE SCOPE IN PHP PHP variables can have various scopes (the scope of a variable determines the perimeter within which the variable can be used). Most variables have a global scope (that of the current file, plus all the files which are included or required -more about this later-). However, it can happen that a variable is used only locally (local variables): this is for instance the case for variables which are used within a function (you can learn about the definition of functions in another PHP tutorial). Have a look at the example below: Learn the PHP code: <html> <body> <?php $var1 = 1; function print_between_stars() { echo '*'.$var1.'*'; } print_between_stars(); ?> </body> </html> Run the PHP script in your web browser: Explanation: In the example above, $var1 is considered as a local variable within the function print_between_stars(). Therefore, its previous definition won't be taken into account, so that calling the function print_between_stars() will return nothing but two stars (along with a warning stating that $var1 hasn't been set). If you want to be able to use the first instantiation of $var1 within the function, you must first declare it as global within the function: Learn the PHP code: <html> $var1 = 1; function print_between_stars() { global $var1; echo '*'.$var1.'*'; } print_between_stars(); <body> <?php ?> </body> </html> Run the PHP script in the web browser: Remark: Since the PHP variable $var1 has been declared global, its initial value has been passed on successfully at the function level. Static variables: A static variable is local within a function but keeps its value after the function has been executed (unlike purely local functions which are reinitialized upon execution of the function, i.e. after a change in scope)). Learn the PHP code: <html> <body> <?php function print_between_stars() { static $var = 0; $var = $var + 1; echo $var; } print_between_stars(); print_between_stars(); ?> </body> </html> PHP Constants: A constant is a name to which is assigned a constant value (constant because it won't be able to be changed during the script execution; additionally, a constant automatically has a global scope). It is defined according to the syntax define("CONSTANT_NAME",VALUE), where CONSTANT_NAME is the name of your constant and VALUE its value. Learn the PHP code: <html> <body> <?php define("MY_CONSTANT",2); echo MY_CONSTANT; ?> </body> </html> Run the PHP script in your web browser: Reserved predefined variables: PHP allows you to access various reserved predefined variables; for instance, $GLOBAL is an array which contains all the global variables used within the script, the keys of this array being the names of these global variables. The $GLOBAL variable, like the other reserved predefined variables (some of which will be introduced later in the next PHP tutorials), is automatically a global variable, meaning that it can be accessed from anywhere in the script. For this reason, it is called a superglobal variable. For instance, one of our examples above could be rewritten: Learn the PHP code: <html> <body> <?php $var1 = 1; function print_between_stars() { echo '*'.$GLOBALS['var1'].'*'; } print_between_stars(); ?> </body> </html> The result returned is the same as if the variable $var1 had been declared global by using the keyword global within the function. PHP VARIABLES - VARIABLE VARIABLES IN PHP A variable variable allows you to dynamically create new PHP variables as the PHP script is being executed; it has the form $$var, where $var denotes a usual PHP variable whose value will become the name of a new variable. The PHP script below illustrates how you can generate new variables thanks to variable variables: Learn the PHP code: <html> <body> <?php $var = 'new'; $$var = 'new variable definition'; echo $new; ?> </body> </html> Run the PHP script in your web browser: Remarks: A new PHP variable called $new has been created by the variable variable $$var. In the same way, it is possible to define variable variable variables (of the form $$$var), etc ... this is done iteratively starting from the definition of a variable variable. Note: A PHP variable can have different types but no definition of type is needed since it is automatically determined when the variable is assigned a value: PHP is indeed a very weakly typed programming language, which offers you great flexibility but also requires great attention (because it is easier to make mistakes when types are not defined beforehand by the programmer).A PHP variable can be assigned by value (in which case the PHP variable simply stores a value) or by reference (in which case the PHP variable stores a pointer to a location in memory; therefore, the value of the variable will change whenever the value located at this memory address is changed).A PHP variable can have different scopes (local, global) and there exist some reserved predefined variables which can be used anywhere in the script: because such variables are automatically global, they are called superglobals (or superglobal variables).Variable variables can be used to dynamically generate new PHP variables as the execution of your PHP script unfolds: it can prove to be practical whenever you do not know beforehand the names of the variables that will have to be generated. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.