PHP

Lecture

PHP For Beginners

Basic Syntax

Invoke PHP in HTML:
1
<?php
2
echo "Hello world";
3
?>
Copied!
PHP commands must end with a semicolon.
Single-line comment:
1
// This is a comment
Copied!
Multi-line comment:
1
/* This is a section
2
of multiline comments
3
which will not be
4
interpreted */
Copied!
Define a variable:
1
$x = 10;
Copied!

Variables

String

Define a string variable:
1
$username = "Fred Smith";
Copied!
Print variable on the screen without a return value:
1
echo $username;
Copied!
Print variable on the screen with a return value:
1
print $username;
Copied!
Code example:
1
<?php // test1.php
2
$username = "Fred Smith";
3
echo $username;
4
echo "<br>";
5
$current_user = $username;
6
echo $current_user;
Copied!

Numeric

Integer:
1
$count = 17;
Copied!
Floating-point:
1
$count = 17.5;
Copied!

Array

Array of strings:
1
$team = array('Bill', 'Mary', 'Mike', 'Chris', 'Anne');
Copied!
Two-dimensional array:
1
$oxo = array(array('x', ' ', 'o'),
2
array('o', 'o', 'x'),
3
array('x', 'o', ' '));
Copied!

Operators

Is equal to:
1
$j == 4
Copied!
Is not equal to:
1
$j != 21
2
$j <> 23
Copied!
Is identical to:
1
$j === "987"
Copied!
And:
1
$j == 3 && $k == 2 // High precedence
2
$j == 3 and $k == 2 // Low precedence
Copied!
Or:
1
$j < 5 || $j > 10 // High precedence
2
$j < 5 or $j > 10 // Low precedence
Copied!
Xor:
1
$j xor $k
Copied!

Variable Assignment

Variable Incrementing and Decrementing

Increment:
1
++$x;
2
$x++;
Copied!
Decrement:
1
--$y;
2
$y++;
Copied!
In if statement, ++$x == 10 means first increment x then compare it with 10, $x++ == 10 means first compare x with 10 then increment it.

String Concatenation

Concatenate strings:
1
echo "You have " . $msgs . " messages.";
Copied!
Append a string to a variable:
1
$bulletin .= $newsflash;
Copied!

String Types

Without variable substitution (single quotes):
1
$info = 'Preface variables with a $ like this: $variable';
Copied!
With variable substitution (double quotes):
1
echo "This week $count people have viewed your profile";
Copied!

Escaping Characters

Escape quote:
1
$text = 'My spelling\'s still atroshus';
Copied!
Tab:
1
$heading = "Date\tName\tPayment";
Copied!

Multiline Commands

Multiline string

1
<?php
2
$author = "Steve Ballmer";
3
4
echo "Developers, developers, developers, developers, developers,
5
developers, developers, developers, developers!
6
7
- $author."
8
?>
Copied!

heredoc

1
<?php
2
$author = "Brian W. Kernighan";
3
4
echo <<<_END
5
Debugging is twice as hard as writing the code in the first place.
6
Therefore, if you write the code as cleverly as possible, you are,
7
by definition, not smart enough to debug it.
8
9
- $author.
10
_END;
11
?>
Copied!
Remember that the closing _END; must appear right at the start of a new line, and it must be the only thing on that line.

Variable Typing

PHP is a loosely typed langauge. This means that variables do not have to be declared before they are used and that PHP always converts variables to the type required by their context when they are accessed.
Automatic conversion from a number to a string:
1
$number = 12345 * 67890;
2
echo substr($number, 3, 1)
Copied!