Vous êtes sur la page 1sur 33

PHP - HYPERTEXT

PREPROCESSOR
Jan Carlo Arroyo
What is PHP?
 Server-Side Scripting Language
 Works with HTML
 Provides more flexibility than HTML
 Syntax is similar to C and Perl
Why Use PHP?
 Open Source / Free software
 Cross Platform to develop, deploy and use
 Powerful, robust, scalable
 Web development specific
 Can be Object Oriented
 Has a lot of documentations
 www.php.net

 Large Community
What do you need?
 Web Server (Apache)
 PHP (language)
 Text Editor (Notepad++ / Dreamweaver)
 Web Browser (Google Chrome / Mozilla Firefox)
 Database (MySQL)
Windows Installation
XAMPP
Writing PHP
 starts with <?php
 Ends with ?>
Localhost – PHP Info
 Make sure your web server is running

<?php phpinfo(); ?>


Echo
 Used to print
 <?php echo “Hello World!”; ?>
 You may print html tags too
 <?php echo “Hello<br>World!”; ?>
Operational Trail
Operational Trail
Writing Comments
 Single line comments
 //

#

 Double line comments


 /* Line 1
Line 2
*/
PHP Case Sensitivity
 all user-defined functions, classes, and keywords
(e.g. if, else, while, echo, etc.) are case-insensitive.
 However; in PHP, all variables are case-sensitive.
PHP Data Types
 PHP Strings
 A string is a sequence of characters, like "Hello world!".
 A string can be any text inside quotes. You can use single or
double quotes
 PHP Integers
 An integer is a number without decimals.
 Rules for integers:
◼ An integer must have at least one digit (0-9)
◼ An integer cannot contain comma or blanks
◼ An integer must not have a decimal point
◼ An integer can be either positive or negative
PHP Data Types
 PHP Floating Point Numbers
 A floating point number is a number with a decimal point or
a number in exponential form.
 PHP Booleans
 Booleans can be either TRUE or FALSE
 PHP NULL Value
 The special NULL value represents that a variable has no
value. NULL is the only possible value of data type NULL.
 The NULL value identifies whether a variable is empty or
not. Also useful to differentiate between the empty string
and null values of databases.
 var_dump() – function to display data type and value
Variables
 Symbolic representation of a certain value
Variables - Examples
Using Variables
 $item = 7.50;
 $item= “8.50”;
 $myString = “Hello World”;
PHP Constants
 A constant is an identifier (name) for a simple value.
The value cannot be changed during the script.
 A valid constant name starts with a letter or
underscore (no $ sign before the constant name).
 Define(“NAME”, “VALUE”,CASE-INSENSITIVITY)
 Note: Unlike variables, constants are automatically
global across the entire script.
PHP Arithmetic Operators

Operator Name Example Result


+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and
$y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x
divided by $y
PHP Assignment Operators

Assignm Same as... Description


ent
x=y x=y The left operand gets set to the value of
the expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
PHP String Operators

Operator Name Example Result


. Concatenation $txt1 = "Hello" Now $txt2 contains
$txt2 = $txt1 . "Hello world!"
" world!"
.= Concatenation $txt1 = "Hello" Now $txt1 contains
assignment $txt1 .= " "Hello world!"
world!"

$x=“Hello”;
$y=“World”;
PHP Increment / Decrement Operators

Operator Name Description


++$x Pre- Increments $x by one, then returns $x
increment
$x++ Post- Returns $x, then increments $x by one
increment
--$x Pre- Decrements $x by one, then returns $x
decrement
$x-- Post- Returns $x, then decrements $x by one
decrement
PHP Comparison Operators
Operator Name Example Result
== Equal $x == $y True if $x is equal to $y

=== Identical $x === $y True if $x is equal to $y,


and they are of the same
type
!= Not equal $x != $y True if $x is not equal to
$y
!== Not identical $x !== $y True if $x is not equal to
$y, or they are not of the
same type
> Greater than $x > $y True if $x is greater than
$y
< Less than $x < $y True if $x is less than $y

>= Greater than or $x >= $y True if $x is greater than


equal to or equal to $y
<= Less than or equal $x <= $y True if $x is less than or
to equal to $y
Strings
 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.
String Functions
 Concatenate
 Strtolower
 Strtoupper
 Ucfirst
 Ucwords
 Strlen
Working with Numbers
 PHP can perform mathematical operations
2 *2
 (1+5) / 3

 $var1 - $var2

 $var1++

 $var1--
Working with Floats
 Round(variable, #of decimal points)
 Ceil(variable)
 Floor(variable)
Form attributes
 action
 method
Form Methods

 GET method

 POST method
GET

 produces a long string that appears in your server


logs, in the browser’s address box.
 restricted to send about 2000 characters only.

 $_GET[‘variable’]
POST

 does not have any restriction on data size to be sent

 $_POST[‘variable’]

Vous aimerez peut-être aussi