Lesson 2 - PHP Variables
Lesson 2 - PHP Variables
Introduction to PHP
Slide 1
Running PHP Scripts
All php scripts must enclosed
with
<?php … ?> (standard tag)
<? … ?> (short open tag)
<script language=“php”>
….
</script> (script tag)
<% … %> (asp style tag)
OUTPUT:
Slide 19
Running PHP Scripts
opening tag for php script Statement terminator
Slide 20
PHP Comments
Slide 21
PHP Variables
• Variables are used as a container for values that can be used and
manipulate PHP scripts.
• Variables in PHP must begin with a $ symbol.
• Variable might contain a string, numbers, arrays and objects.
• Setting the identifier for a variable must follow some rules.
1. First character can be ‘_’ or a letter.
2. It can only contain alpha-numeric characters and underscore
(a-z,A-Z,0-9,_).
3. Don’t use white space in naming a variable.
• Variables in PHP is not explicitly declared
• Variables are case sensitive
Example
$age, $firstName, $totalSalary, $_myValue,
$str1, $tmp01
Slide 22
Using ‘ ‘ (pair single quote), “ “ (pair double
quotes) and . (dot character) PHP
1. Strings inside the ‘ and ‘ are interpreted as literal strings
1. To display a ‘ character (single quote) as string use the
escape character \ (backslash).
2. Strings inside the “ and “ are interpreted as literal strings with
some exceptions
1. $ inside “ and “ are interpreted as variables thus it will
display the value rather than the string that starts with $.
2. To display the string that starts with $ and to display the
double quote as string use the escape character \
(backslash).
3. To concatenate string values use the . (dot or period)
character thus “abc“.”def” yields to a value “abcdef”
Slide 23
Using ‘ ‘ (pair single quote), “ “ (pair double
quotes) and . (dot character) PHP
Example: Output:
Slide 24
Outputting Data
echo syntax
void echo(string argument1[,…string argumentN]
print syntax
int print(argument)
printf syntax
boolean printf(string format [, mixed args])
Slide 25
Outputting Data
Example:
Output:
Slide 26
Commonly Used Type Specifiers for printf function
Beginning PHP and MySQL 3rd Edition by: W. Jason Gilmore pp.100
Slide 27