ITEC 325 - PHP (PHP Third Edition by Ullman)
Chapter 2 Lecture Notes
Based on notes by Jack Davis (jcdavis@radford.edu)
- PHP Variables
- PHP is a dynamically typed language, so variables don't have to be declared.
The type of the variable is determined by its usage in code.
- Variable Types - boolean, integer, float, string, array, object,
resource, NULL
- Variable Syntax
- All variable names must be preceded by a dollar sign ($).
- Following the dollar sign, the variable name must begin with either a
letter (A-Z, a-z) or an underscore (_). It can't begin with a number.
- The rest of the variable name can contain any combination of letters,
underscores, and numbers.
- May not have spaces within the name of a variable.
- Variable names are case sensitive.
- By convention, almost all php programmers use lower case
variable names, reserving uppercase names for files, resources,
and constants.
- Numbers
- floats (actually doubles) and integers
- valid number literals,
12.2, 44, -1, -1.2
- use:
$count = 22; $ctr = 13.3; ...
- Strings
- A string is any number of characters enclosed within a pair of either
single (') or double (") quotation marks. Strings can contain
any combination of letters, numbers, symbols, and spaces.
-
Double-quoted
strings can also contain variables. Extrapolation (only) occurs within
double quoted strings.
- String examples:
$str = "How are you?";
$first_name = "John";
$str = "How are you $first_name?";
$date = "1/01/2010";
$str2 = "I said, \"how are you?\"";
- Simple example
https://php.radford.edu/~ibarland/ITEC325/2011spring-ibarland/Lectures/php/simple.php
- Arrays
- index array
$arr = array("john","mary","zimbo");
$arr[] = "betty";
-
associative array
$crsarr = array("web"=>325, "java1"=>120);
$crsarr["discretemath"] = 122;
- Constants
define ("pi", 3.1415926535);
The name of the constant is always quoted, but the value you assign
to the constant is only quoted if it is a string literal. When you use
a constant name in code you don't precede it with a $.
<?php
define ("PI",3.14152926535);
echo "The constant PI holds ", PI, ";<br />"; ?>
- PHP Predefined Variables ($_SERVER array)
https://php.radford.edu/~ibarland/ITEC325/2011spring-ibarland/Lectures/php/pre-defined-vars.php
-
Booleans:
A note on
implicit boolean conversions.