Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL

Home > PHP > Expressions and Operatorsprinter version

PHP Expressions and Operators

Expressions in PHP are formed from literal values (i.e. integers, floats, strings etc.), operators and the return values of function calls.

An example of an expression with two literals (in this case integers) separated by an operator would be $my_var = 4 + 5;.

When two literals of different types (e.g. integer and float) are used then PHP will automatically type the result. For example $my_var = 4.0 + 5; will be a float type.

Operator Precedence

In common with other programming languages, PHP gives precedence to multiplication and division over addition and subtraction etc.

Memorising and using the order of operator precedence usually leads to mistakes and unreadable code therefore the easiest way is to use parenthesis.

For example $my_variable = 3 + 4 * 2; means that $my_variable = 11;. However, $my_variable = 3 + (4 * 2); is less error prone and much clearer.

Type Conversion

PHP has several methods that allow variables of one type to be converted to another. The following functions explicitly convert types.

$var = intval("10"); converts the string "10" to the integer 10.
$var = strval(10.2); converts the float 10.2 to the string "10.2".
$var = floatval(10); converts the integer 10 the float 10.0.

Type-Casting

PHP also supports type-casting whereby the desired type is placed in parentheses in front of the variable.



Examples of Type-Casting
Value of $var(int) $var(float) $var(bool) $var(string) $var
10.81010.8true"10.8"
000false"0"
"9"99true"9"
"5 inches"55true"5 inches"
true11true"1"
false00false""


Automatic Type Conversion

PHP will automatically convert the type of a variable if two different types are combined in an expression or if a variable is passed to a library function that expects a different type.

The same rules are applied as for type-casting.
Example $var = "10" + 0.2; will set $var as a float equal to 10.2.

Finding a Variable's Type

Because PHP is a loosely typed language the following functions can be used to check a variable's type - returning either true or false.



Finding a Variable's Content

PHP provides two means to test the contents of a variable.



These appear to be the same but they're not.
For example if $var = 0 then both isset($var) and empty($var) return true.
Whereas if $var = NULL then isset($var) returns false but $empty($var) returns true.

A variable can be destroyed by using unset() e.g. unset($var) will set $var equal to NULL.

Previous - PHP variables Previous - Variables     Next - Strings Next - PHP strings


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
PHPBasicsCreating a ScriptRunning a ScriptVariablesExpressions and OperatorsExpressions and OperatorsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook
Tips and TricksPage TemplatesForm Reply ScriptsForm ValidationJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.