PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
Variables and Assignment Operations
This section describes what is a variable, variable names prefixed with $, 'set' and 'unset' states of a variable, assigning data to variables, var_dump($var) to print variable information.
What is a variable? A variable is an identifier for a piece of data stored in memory during the program execution. In PHP, variables are used under these basic rules:
1. A variable name in PHP must be prefixed with a "$" sign. Perl has a similar rule for scale variables.
2. Variable names are case sensitive. So $address and $Address refer to two different variables.
3. Variables in PHP are typeless. Therefor there is no variable type declaration statements in PHP. Any variable can be used as the identifier for any data type.
4. A variable has 2 states: "set" and "unset". A variable is in the "set" state, if a piece of data has been assigned to it. A variable is in the "unset" state, if there is no data assigned to it.
5. Assignment operations (coded by the assignment operator, =) can be used to assign data to variables.
6. The unset($var) function can be used to remove the assigned data from the given variable.
7. Variables can used in any operations like data literals in PHP source code. If a variable with data assigned is used in an operation, the assigned data value will be used for the operation. If a variable with no data assigned is used in an operation, NULL will be used for the operation.
8. There are some special characters that you should not use them as part of a variable name, ' ', '$', '[', ']', '{', '}', ...
PHP has a nice built-in function, var_dump($var), that prints out detailed information about the specified variable.
To show you some of variable rules mentioned above, I wrote the following PHP script, VariableTest.php:
<?php # VariableTest.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # print "\nVariable assignments:\n"; $u; # Not assigning to any data print "\n \$u: "; var_dump($u); $b = TRUE; # Assigning a boolean print "\n \$b: "; var_dump($b); $i = 777; # Assigning an integer print "\n \$i: "; var_dump($i); $f = 3.14; # Assigning a float print "\n \$f: "; var_dump($f); $s = "Hello Herong"; # Assigning a string print "\n \$s: "; var_dump($s); $a = array("Jan"=>31,"Feb"=>28); # Assigning an array print "\n \$a: "; var_dump($a); $o = new DateTime(); # Assigning an object print "\n \$o: "; var_dump($o); $r = opendir("."); # Assigning a resource print "\n \$r: "; var_dump($r); $n = NULL; # Assigning NULL print "\n \$n: "; var_dump($n); ?>
If you run this sample script, you should get:
herong> \php\php VariableTest.php Variable assignments: $u: NULL $b: bool(true) $i: int(777) $f: float(3.14) $s: string(12) "Hello Herong" $a: array(3) { ["Jan"]=> int(31) ["Feb"]=> int(28) ["Mar"]=> int(31) } $o: object(DateTime)#1 (0) { } $r: resource(4) of type (stream) $n: NULL
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
►Variables, References, and Constants
►Variables and Assignment Operations
Variable Variable Name - Name Variables with Expressions
Constant and define() Function
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS