Variables and Expressions in PHP
By the end of this post you will be able to:
- Demonstrate how to use identifiers in PHP
- Identify datatypes (float, integer, string etc)
- Create variables in PHP
- Identify global constants in PHP
What are PHP Identifiers/Variables?
An identifier is a general term or custom name applied to variables, functions and various other user defined objects.
The word identifier/variable is interchangeable in programming. The best way to think of a variable is like a bucket that contains specific contents.
When defining variables you must note the following
Variables must begin with a letter or underscore _variableName You can camelCase variables Identifiers can_be_any_length
PHP has it’s own reserved variables
Identifiers must not have the same name as PHP’s own identifier names. A reference of PHP variable names can be located here: php.net/manual/en/reserved.variables.php
Create your own variables
Use this code as a starting point – change the variable name to display in browser.
<?php //Variables example 1 $firstName = "Toby"; $lastName = "Harris"; /*You can echo out your variable without using speech marks. Beware if you use single quotations it will not display the value of the variable but just the name of it. Try it and see*/ echo "$firstName $lastName"; ?>
What’s happening here?
- All PHP identifiers must be prefixed by the ‘$’ symbol.
- We assign a value to the identifier or variable by using the ‘=‘ sign.
- You can echo out the variable without the need to wrap it in quotation or speech marks
- Or you can include the variable as part of a string
Data Types
What are data types?
- Data types are containers
- Each container is designed to hold different types of content
Most commonly used data types
String: Alphanumeric characters Float: Decimal Integer: Whole numbers Boolean: True or false Arrays: Lists of items (Explored in lesson 6) Null: Nothing or zero
How are data types defined in PHP
PHP is referred to as a loosely type language:
- Any identifier can hold any type of data
- You do not need to declare the type data an identifier will hold
Let’s see some variables in action
Consider the code:
- How are the variables defined?
- How are they echoed out on to the screen?
$var1 = 'PHP'; // Assigns a value of 'PHP' to $var1 $var2 = 5; // Assigns a value of 5 to $var2 $var3 = $var2 + 1; // Assigns a value of 6 to $var3 $var2 = $var1; // Assigns a value of 'PHP' to $var2 echo "<ul>"; echo "<li>$var1 </li>"; // Outputs 'PHP' echo "<li>$var2 </li>"; // Outputs 'PHP' echo "<li>$var3 </li>"; // Outputs '6' echo '<li>' . $var1 . ' is ace!</>'; // Outputs 'PHP is ace' echo "<li>$var1 is ace! </li>"; // Outputs 'PHP is ace!' echo '<li>$var1 is ace! </li>'; // Outputs '$var1 is ace!' echo "</ul>"
A quick look at global constants and scope
Global constants
- Global constants keep their state throughout the entire execution of a script
- Example: Setting a VAT or shipping rate that applies throughout an application
- Global constants by convention are defined in uppercase:
define: ('VAT_RATE', 21.0 );
or
VATRATE
Some useful links
- Complete anthology of PHP variables: http://php.net/manual/en/language.types.php
- Useful example on global constants: php.net/manual/en/language.constants.php
What have we explored in this post?
- Demonstrate how to use identifiers in a PHP script
- Identify data types in PHP (float, integer, string etc)
- Create variables in PHP
- Identify predefined variables in PHP (More in future lessons)
No Comments