index act articles resource 16 ext iframe

Building Blocks of PHP

Understand the linguistic terms of PHP, including boolean, operators, variables and constants.




Building Blocks of PHP
1298 Views
Variables


A variable is a special container that you can define, which will then contain a value you specify. For example a number, string, object, array or boolean.




Constants


If you want to work with a value that needs to remain unchanged, you can define and use a constant variable. This isdifferent to a variable as variables offer a flexible way of storing data as you can change their values and type of data they hold.




Globals and Superglobals


In addition to global declerations aof your own, PHP has built in predefined variables called superglobals. These are always present and the value they hold can be available in all your scripts.




Predefined Constants


PHP provides some built in constants for you. For example __FILE__ returns the name of the file that PHP is using. __LINE__ returns the line number that php is running and PHP_VERSION returns what php version your script is being run on.




Data types


Different types of data will take up different amounts of memory and may then be treated different in the script that you write. PHP will automatically determine the data type for the variable the time data is assigned to it. Some examples of data types are boolean, integer, float, string, object, array, resource, null (an uninitialized value).




The Assignment operator


The equals sign (=) is an assignment operator and will put the value on the right hand site and assign it to the left hand side.




Arithmetic Operators


Arithmetic operators perform mathematic operations on the values you want them to (providing they are a number). Some examples are addition (+), subtraction (-), division (/), multiplication (*), modulus (%). You can increment numbers by putting two addition operators nxet to eachother like so: (++). PHP provides combined operators which allow you to transform the left hand operand and return a result, while also modifying the original value of the variable. To return ‘x’ and and 5 to ‘x’ on the same line you would use ‘$x +=5.’




The Concatanation Operator


The concatanation operator allows you to join strings together. It is represented by a single period (.). Expressions, calculations and variables can also be concataned and the data type returned will aloways be that of a string.




The Comparison Operator


Comparison operators perform comparative tests using their operands and return the boolean value true if the test is succesful and false if it false. Some examples are Equivalence (==), Non-equivalence (!=), greater than (>), less than or equal to (<=). You can use logical operators such as Or (||), and (&&) and Not (!) to extend comparisons using the if() statement.
Building Blocks of PHP