2. Part I: Understanding PHP Basics
Using Variables and Operators
Prepared by KhmerCourse
3. Storing Data in Variables
៚ Some simple rules for naming variables
៙ Be preceded with a dollar symbol $
៙ And begin with a letter or underscore _
៙ Optionally followed by more letters, numbers, or underscore
៙ Be not permitted
ៜ Punctuation: commas ,, quotation marks ?, or periods .
ៜ Spaces
៙ e.g.
ៜ $id, $_name and $query3: valid
ៜ $96, $day. and email: invalid
៚ Variable names are case-sensitive.
៙ e.g. $name and $Name refer to different variables.
3
4. Assigning Values to Variables
៚ $var = val;
៙ e.g. assigningValues2Variables.php
<?php $language = "PHP"; ?>
<h1>Welcome <?php echo $language; ?></h1>
៚ Dynamic variable's name
៙ e.g. dynamicVariableName.php
<?php
$clone = "real";
// create a
value of
${$clone} =
echo $real;
?>
new variable dynamically
$clone
"REAL";
// output: REAL
at run time from the
Is it possible for a variable's name itself to be a variable?
៚ echo(): print the value of a variable
4
7. Understanding PHP’s Data Types
៚ Data type is the values assigned to a variable.
៚ Booleans
៙ 1 (true) or 0 (false)
៚ 2 numeric
៙ Floating-point values (a.k.a floats or doubles) are decimal or fractional
numbers,
៙ While integers are round numbers.
៚ Non-numeric: String
៙ Be enclosed in single quotes (') or double quotes (")
៚ NULL (a special data type in PHP4)
៙ Represent empty variables; a variable of type NULL is a variable without
any data.
A NULL value is not equivalent to an empty string "".
7
10. Setting and Checking Variable Data Types (cont.)
៚ e.g. casting.php
<?php
$f_speed =
$i_speed =
// output:
36.9; // floating-point
(integer)
36.9
$f_speed; // cast to integer
echo $f_speed;
// output: 36
echo
?>
$i_speed;;
10
Data Type Checking Functions
Function Purpose
is_bool Test if holding a Boolean value
is_numeric Test if holding a numeric value
is_int Test if holding an integer value
is_float Test if holding a float value
is_string Test if holding a string value
is_null Test if holding a NULL value
is_array Test if being an array
is_object Test if being an object
11. Using Constants
៚ define(CONST, val);
៚ Constant names follows the same rules as variable names but not the $
៚ e.g. usingConstants.php
<?php
define("APPLE", "Apple");
define("YR", 2011);
prefix.
// output: Apple 2011
echo
?>
APPLE." ".YR;
Constants name are usually entirely UPPERCASED.
When should we use a variable, and when should we use a constant?
11
12. Manipulating Variables with Operators
៚ Operators are symbols that tell the PHP processor to perform certain actions.
៚ PHP supports more than 50 such operators, ranging from operators for
arithmetical operations to operators for logical comparison and bitwise
calculations.
12
13. Performing Arithmetic Operations
៚ e.g. arithmeticOperations.php
<?php
echo 3 + 2; // output: 5
echo 3 - 2; // output: 1
echo 3 * 2; // output: 6
echo 3 / 2; // output: 1.5
echo
?>
3 % 2; // output: 1
Is there any limit on how large a PHP integer value can be?
13
Arithmetic Operators
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
14. Concatenating Strings
៚ e.g. concatenatingStrings.php
<?php
$apple = "Apple";
$banana = "Banana";
// use (.) to join strings into 1
$fruits = $apple." and ".$banana;
// output: I love apple and
".$fruits.".";
banana..
echo
?>
"I love
14
15. Comparing Variables
៚ e.g. comparingVariables.php
<?php
$num = 6; $num2 = 3; $str = "6";
// output:
echo ($num
// output:
echo ($num
0
<
1
>
(false)
$num2);
(true)
$num2);
// output:
echo ($num
0
<
(false)
$str);
// output:
echo ($num
// output:
echo ($num
?>
1 (true)
== $str);
0 (false)
=== $str);
15
Comparison Operators
Operator Description
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
=== Equal to and of the same type
18. Understanding Operator Precedence
៚ Operators at the same level have equal precedence:
៙ ++
៙ !
៙ *
៙ +
៙ <
៙ ==
៙ &&
៙ ||
៙ =
--
/
-
<=
!=
%
.
> >=
=== !==
+= -= *= /= .= %= &= |= ^=
៚ Parentheses (
៚ e.g.
៙ 3 + 2 *
៙ (3 + 2)
): force PHP to evaluate it first
5; // 3 + 10 = 13
* 5; // 5 * 5 = 25
18
19. Handling Form Input
៚ e.g. chooseCar.html
<form name="fCar" method="POST" action="getCar.php">
<select name="selType">
<option value="Porsche">Porsche</option>
<option value="Ford">Ford</option>
</select>
Color:
<input
<input
</form>
type="text" name="txtColor" />
type="submit" value="get Car" />
action="getCar.php"
Reference a PHP script
method="POST"
Submission via POST
GET: method="GET"
19
20. Handling Form Input (cont.)
៚ e.g. getCar.php
<?php
// get values via $_POST | $_GET
$type = $_POST["selType"];
$color = $_POST["txtColor"];
echo $color." ".$type;
?>
$_POST[fieldName];
$_POST: a special container variable (array) is used to get a value of a field
of a form sent by using the POST method (or $_GET for the GET method).
fieldName: the field whose value will be get/assigned to a variable.
20