2. 2
Introduction
● What is “PHP” ?
– PHP is an HTML-embedded scripting language.
– PHP is a server side scripting language.
– PHP syntax is borrowed from C, Java and Perl with a couple
of unique PHP-specific features thrown in.
– The goal of the language is to allow web developers to write
dynamically generated pages quickly.
3. 3
Introduction
● What PHP do?
– When someone visits your PHP webpage, your web server
processes the PHP code.
– It then sees which parts it needs to show to visitors(content
and pictures) and hides the other stuff (file operations, math
calculations, etc.) then translates your PHP into HTML.
– After the translation into HTML, it sends the webpage to
your visitor's web browser.
5. 5
Introduction
● What you need to learn PHP?
– Software
● Web server (Apache/Nginx/IIS)
● PHP itself
● PHP code editor (notepad++,context, etc.)
● Database Server (MySQL/SQLite/SQLServer)
– User skill
● Know HTML syntax
● Basic programming knowledge (not necesary)
7. 7
Introduction
● Common use of PHP
– Performs system functions, i.e. from files on a system it can
create, open, read, write, and close them.
– Can handle forms, i.e. gather data from files, save data to a file,
thru email you can send data, return data to the user.
– Add, delete, modify elements within your database thru PHP.
– Access cookies variables and set cookies.
– Using PHP, you can restrict users to access some pages of your
website.
– It can encrypt data.
8. 8
Installing PHP
● For Microsoft Windows user
– Use XAMPP/WAMPP for all in one web server
(PHP, MySQL, Apache, phpMyAdmin)
– Use Microsoft web server (IIS)
● For Linux user
– Refers to your linux distribution repository or install one by
one (PHP, MySQL, Apache, phpMyAdmin)
– Using lamp package
9. 9
How to save PHP files?
● Always save the file with a .php extension instead of .html
● Do not use word processor applications to create a php files.
● Always use pure text editor such as notepad++, context, or
specific IDE for PHP (komodo, crimson, aptana studio, etc.)
10. 10
PHP - Syntax
● What is “Syntax” ?
– The rules that must be followed to write properly structured
code.
● Standard syntax of PHP
– <?php
?>
● Non-standard syntax of PHP (not recommended)
– <?
?>
11. 11
PHP - Syntax
● Semicolon
– The semicolon signifies the end of a PHP statement and
should never be forgotten.
● White space
– Whitespace is ignored between PHP statements.
– This means it is OK to have one line of PHP code, then 20
lines of blank space before the next line of PHP code.
– You can also press tab to indent your code and the PHP
interpreter will ignore those spaces as well.
12. 12
Sample of PHP code
<?php
echo "Hello World!";
echo "Hello World!";
?>
Will display in browser:
Hello World!Hello World!
13. 13
PHP Syntax
● Use “ . “ (dot) to combine token
– Token is the smallest part of PHP
● Example
– Numbers (124583)
– Variables
– Constants
● Braces make blocks
if (3 == 2 + 1)
{
echo "Good - I haven't totally";
echo "lost my mind.<br>";
}
15. 15
PHP Variables
● What is Variables?
– A place in computer memory for storing a value (text/number)
● Variables can, but do not need, to be declared before assignment.
● Variables used before they are assigned have default values.
● PHP does a good job of automatically converting types from one to another when
necessary.
● PHP variables are Perl-like.
● Defining the variable
– $variable_name = Value;
– Example
● $name=”Kris”;
● $midTerm=77;
● $final_grade=”A”;
16. 16
PHP Variables
● Naming conventions
– must start with a letter or underscore "_"
– may only be comprised of alpha-numeric characters and
underscores. a-z, A-Z, 0-9, or _
– Variables with more than one word should be separated with
underscores (ex. $my_variable)
– Variables with more than one word can also be distinguished
with capitalization (ex. $myVariable)
– Php variables are case sensitive !
17. 17
PHP data types
● Integers: are whole numbers, without a decimal point, like 4195.
● Doubles: are floating-point numbers, like 3.14159 or 49.1.
● Booleans: have only two possible values either true or false.
● NULL: is a special type that only has one value: NULL.
● Strings: are sequences of characters, like 'PHP supports string operations.'
● Arrays: are named and indexed collections of other values.
● Objects: are instances of programmer-defined classes, which can package
up both other kinds of values and functions that are specific to the class.
● Resources: are special variables that hold references to resources external
to PHP (such as database connections).
18. 18
Output command
● Echo command
– To send an output to screen, use 'echo' command
– It can be used for variable or even a quotation string
– ex.
<?php
$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";
?>
19. 19
Echo attention
● Be careful when using HTML code or any other string that
includes quotes
● Use one of the following tactics if your string contains
quotations:
– Don't use quotes inside your string
– Escape your quotes that are within the string with a
backslash. To escape a quote just place a backslash directly
before the quotation mark, i.e. "
– Use single quotes (apostrophes) for quotes inside your string.
20. 20
Echo attention
● Ex.
<?php
echo "<h5 class="specialH5">I love using PHP!</h5>";
echo "<h5 class='specialH5'>I love using PHP!</h5>";
?>
22. 22
Example
$addition = 3 + 5;
$subtraction = 4 - 2;
$multiplication = 5 * 6;
$division = 8 / 2;
$modulus = 3 % 2;
echo "Perform addition: 3 + 5 = ".$addition."<br />";
echo "Perform subtraction: 4 - 2 = ".$subtraction."<br />";
echo "Perform multiplication: 5 * 6 = ".$multiplication."<br />";
echo "Perform division: 8 / 2 = ".$division."<br />";
echo "Perform modulus: 3 % 2 = " . $modulus
. ". Modulus is the remainder after the division operation has been performed.
In this case it was 3 / 2, which has a remainder of 1.";
23. 23
PHP Operator
● Comparison Operator
Operator Meaning Example
== Equal to $x == $y
!= Not equal to $x != $y
< Less then $x < $y
> Greater then $x > $y
<= Less or equal to $x <= $y
>= Greater or equal to $x >= $y
24. 24
PHP Operator
● String operator → “ . “ (dot)
– The period is the concatenation operator for strings
● Ex.
$a = “Kris”;
$b = “John”;
echo $a . “ and “ . $b;
26. 26
PHP Operator
● pre/post-increment/decrement
● Ex.
$x = 4;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;
$x = 4;
echo "<br />The value of x with with pre-plusplus = " . ++$x;
echo "<br /> The value of x after the pre-plusplus is " . $x;
27. 27
PHP If Statement
● IF
– IF <expression>
statement;
● IF … ELSE
– IF <expression>
statement_true;
ELSE
statement_false;
28. 28
Excercise
● Show “Hello, John.” in the browser
● Show “I'm learning PHP” in the browser
● Show “Twinkle, Twinkle little star.” in the browser
● Show “What goes around, comes around.” (use variable to print 'around' word)
● Create the following variables:
– $x=10
– $y=5
● Write php code to print out the following
– 10 + 5 = 15
– 10 – 5 = 5
– 10 * 5 = 50
– 10 / 5 = 2
– 10 % 5 = 0
29. 29
Excercise
● Create php script that print like the following:
– Value is now 8.
– Add 2. Value is now 10.
– Subtract 4. Value is now 6.
– Multiply by 5. Value is now 30.
– Divide by 3. Value is now 10.
– Increment value by one. Value is now 11.
– Decrement value by one. Value is now 10.
Hint: use arithmetic-assignment operator