php basics
DESCRIPTION
A quick introduction to the PHP server-side scripting languageTRANSCRIPT
PHP BasicsHenry Osborne
CPTR304: Internet Authoring 2
Syntax
PHP’s syntax derived from many languages
More Java-like with the latest object-oriented additions
Designed primarily as a text processor
CPTR304: Internet Authoring 3
PHP Tags
Standard Tags <? php... code?>
Short Tags <?... code?><?=$variable ?>
Script Tags <script language=“php”... code</script>
ASP Tags <%... code%>
Short tags, script tags and ASP tags are all considered deprecated.
CPTR304: Internet Authoring 4
Comments
// Single line comment # Single line comment
/* Multi-linecomment*/
/** * API Documentation Example * **/function foo($bar) { }
CPTR304: Internet Authoring 5
Whitespaces
You can’t have any whitespaces between <? and php
You cannot break apart keywords You cannot break apart variable names and
function names
CPTR304: Internet Authoring 6
Code Block
{
//Some comments
f(); // a function call
}
CPTR304: Internet Authoring 7
Data Types
boolean true or false
int signed numeric integer value
float signed floating-point value
string collection of binary data
CPTR304: Internet Authoring 8
Numeric Values
Decimal 10; -11; 1452 Standard decimal notation
Octal 0666, 0100 Octal notation – identified by its leading zero and used to mainly express UNIX-style access permissions
Hexadecimal
0x123; 0XFF; -0x100
Base-16 notation
CPTR304: Internet Authoring 9
Numeric Values, cont’d
Decimal 0.12; 1234.43; -.123
Traditional
Exponential
2E7, 1.2e2 Exponential notation – a set of significant digits (mantissa), followed by the case-insensitive letter E and by an exponent.
CPTR304: Internet Authoring 10
Strings
Equivalent to text (according to many programmers)
Actually, an ordered collection of binary data
CPTR304: Internet Authoring 11
Booleans
Used as the basis for logical operations Boolean conversion has special rules:
A number converted to Boolean becomes false if the original number is zero, and true otherwise
A string is converted to false only if it is empty or if it contains the single character 0
When converted to a number or string, a Boolean becomes 1 if true, and 0 otherwise
CPTR304: Internet Authoring 12
Compound Data Types
Arrays are containers of ordered data elements;
Objects are containers of both data and code.
CPTR304: Internet Authoring 13
Other Data Types
NULL – variable has no value
resource – used to indicate external resources that are not used natively by PHP
CPTR304: Internet Authoring 14
Type Conversion
$x = 10.88;
echo (int) $x;
CPTR304: Internet Authoring 15
Variables
A variable can contain any type of data PHP is loosely typed as opposed to being strongly
typed like C, C++, and Java Identified by a dollar sign $, followed by an identifier
name $name = ‘valid’; //valid identifier
$_name = ‘valid’; //valid identifier
$1name = ‘invalid’; //invalid identifier, starts with a number
CPTR304: Internet Authoring 16
Variable Variables
A variable whose name is contained in another variable
$name = ‘foo’;
$$name = ‘bar’;
echo $foo; //Displays ‘bar’
CPTR304: Internet Authoring 17
Variable Variables, cont’d
$name = ‘123’;
/* 123 is your variable name, this would normally be invalid. */
$$name = ‘456’;
// Again, you assign a value
echo ${‘123’};
//Finally, using curly braces you can output ‘456’
CPTR304: Internet Authoring 18
Variable Variables, cont’d
function myFunc() {
echo ‘myFunc!’;
}
$f = ‘myFunc’;
$f(); //will call myFunc();
CPTR304: Internet Authoring 19
Determining If a Variable Exists
Use the special construct isset()
echo isset ($x);
CPTR304: Internet Authoring 20
Referencing Variables
$a = 10;
$b = &$a; //by reference
$b = 20;
echo $a; //Outputs 20
CPTR304: Internet Authoring 21
Constants
define(‘EMAIL’, ‘davey.php.net’); //Valid name
echo EMAIL;
define (‘USE_XML’, true);
if(USE_XML) { }
define (‘1CONSTANT’, ‘some value’); //Invalid
CPTR304: Internet Authoring 22
Operators
Assignment Arithmetic String Comparison Logical
CPTR304: Internet Authoring 23
Additional Operators
Bitwise – manipulating bits using Boolean math
Error Control – error suppressionExecution – executing system commandsIncrementing/DecrementingType – identifying Objects
CPTR304: Internet Authoring 24
String Concatenation
$string = “foo” . “bar”;
$string2 = “baz”;
$string .= $string2;
echo $string;
CPTR304: Internet Authoring 25
Bitwise Operators
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Bitwise left shift
>> Bitwise right shift
CPTR304: Internet Authoring 26
$x = 1;
echo $x << 1; //Outputs 2
echo $x << 2; //Outputs 4
$x = 8;
echo $x >> 1; //Outputs 4
echo $x >> 2; //Outputs 2
CPTR304: Internet Authoring 27
$x = 1;
echo $x << 32; //Outputs 0
echo $x * pow(2, 32); //Outputs 4,294,967,296
CPTR304: Internet Authoring 28
Comparisons
== Equivalence – evaluates to true if both operands have the same value but not necessarily the same type.
=== Identity – evaluates to true only if the operands are of the same type and contain the same value.
!= Not-equivalent – evaluates to true if the operands are not equivalent, without regards to their type.
!== Not-identical – evaluates to true if the operands are not of the same type or contain the same value.
CPTR304: Internet Authoring 29
Logical Operators
&& / and|| / orXOR
CPTR304: Internet Authoring 30
Error Suppression Operator
$x = @mysql_connect();
CPTR304: Internet Authoring 31
Conditional Structures
Decision-making
if-then-else
switch
Iteration
while()
do...while()
for
CPTR304: Internet Authoring 32
if-then-else
if (expression1) {
} elseif (expression2){
} else {
}
CPTR304: Internet Authoring 33
Ternary Operator
echo 10 == $x ? ‘Yes’ : ‘No’;
if (10 == $x){
echo ‘Yes’;
} else {
echo ‘No’;
}
CPTR304: Internet Authoring 34
Switch
$a = 0;
switch ($a) {
case true:
break;
case 0:
break;
default:
break;
}
CPTR304: Internet Authoring 35
while()
$i = 0;
while ($i < 10) {
echo $i . PHP_EOL;
$i++;
}
CPTR304: Internet Authoring 36
do…while()
$i = 0;
do {
echo $i . PHP_EOL;
$i++;
} while ($i < 10)
CPTR304: Internet Authoring 37
for()
for ($i = 0; $i < 10; $i++) {
echo $i . PHP_EOL;
}
CPTR304: Internet Authoring 38
Errors and Error Management
Compile-time errors
Detected by parser while compiling a script. Cannot be trapped within the script itself.
Fatal errors Halt the execution of a script. Cannot be trapped.
Recoverable errors
Represent significant failures but can be handled in a safe way.
Warnings Recoverable errors that indicate a run-time fault. Does not halt execution.
Notices Indicates that an error condition has occurred but is not necessarily significant. Does not halt script execution.
PHP Basics