0% found this document useful (0 votes)
23 views38 pages

PHP Basics

PHP is an open-source, cross-platform scripting language used for web development. It was originally created to generate dynamic web pages but now is used for building entire web applications. PHP code is embedded within HTML and executed server-side to produce dynamic web pages. The document provides an overview of PHP fundamentals including variables, data types, operators, control structures, and form handling.

Uploaded by

yuki tetsu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views38 pages

PHP Basics

PHP is an open-source, cross-platform scripting language used for web development. It was originally created to generate dynamic web pages but now is used for building entire web applications. PHP code is embedded within HTML and executed server-side to produce dynamic web pages. The document provides an overview of PHP fundamentals including variables, data types, operators, control structures, and form handling.

Uploaded by

yuki tetsu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

PHP: AN INTRODUCTION

PHP

 It is an open-source, cross-platform,
interpreted, and server-side scripting language
that is used to create web applications/sites
 Written by Rasmus Lerdorf and originally
known as Personal Homepage/Forms
Interpreter
 Now stands for the recursive acronym “PHP:
Hypertext Preprocessor “
CLIENT SIDE VS SERVER SIDE SCRIPTING
Client-Side Server-Side
1. Executed on the client 1. Runs on a web server.
side.
2. User has access to all the 2. Source code is hidden.
code received by the client.
3. Do not require additional 3.  Require that their
software on the server  language's interpreter be
installed on the server
What do I need to run my PHP script?
Linux or Windows or any OS
- Linux provides the base operating system (OS) and server environment
Apache or IIS (HTTP Server)
The Apache web server intercepts HTTP request and either serves them directly
or passes them on to the PHP interpreter for execution

MySQL
The MySQL RDBMS serves as the data storage engine, accepting connections
from the PHP layer and inserting, modifying, or retrieving data

PHP Package (Authoring tool)


The PHP interpreter parses and executes PHP code, and returns the results to the
web server
PHP ARCHITECTURE
PHP SCRIPT
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php
echo "Hello World!";
?>

</body>
</html>
INSERT PHP IN HTML TAGS
 Four Rules in Coding
1. Every block of PHP starts with <?php and ends
with ?>
2. PHP statements end with a semicolon (;)
3. Text that is not a command must be put in quotes
4. Any time you have a curly bracket or parenthesis,
you should have a matching end bracket or
parenthesis.
EMBEDDING PHP WITHIN HTML
<!DOCTYPE html>
You can insert the PHP scripts in any portions
<html> of the HTML tags. Also you can embed HTML
<head><title>My First PHP Code</title> tags in PHP statements however it will be
</head> treated as text.
<body>
<h2>This creature can change color or to blend in with its surrounding. What is its name?
</h2>
<?php
//display output
echo “<h2>A Chameleon</h2>”;
?>
</body>
</html>
COMMENTS
<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
</body
</html>
PHP: VARIABLE, DATA TYPES, OPERATORS,
CONDITIONS AND ITERATION
VARIABLE
 In PHP, a variable starts with the $ sign and followed by the name of the variable;
 It can have a short name or a more descriptive
 Rules for PHP variables:
 A variable name must start with a letter or the underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and
_)
 Variable names are case-sensitive ($age and $AGE are two different variables)

<?php

$string = "Hello world!";


$x = 5;
$y = 10.5;
echo " <h2>$string</h2> "; //Outputs: Hello World!
echo " x + y = ".$x+$y; //Outputs: x+y=15.5

?>
SINGLE VS DOUBLE QUOTES
 ' ' (Single Quotes)
 Content inside single quotes is evaluated literally.
 Example
$string = 'Single Quotes';
echo '$string';

 " " (double quotes) – Variables inside double quotes are


evaluated for their values.
 Example
$string= " Single Quotes ";
echo " $string ";
DATA TYPES
 It is a loosely type language.
 PHP supports 7 data types:
1. String
2. Integer
3. Float (floating point numbers - also called double)
4. Boolean
5. Array
6. Object
7. NULL
PHP OPERATORS
 Operators are used to perform operations on
variables and values.
 PHP divides the operators in the following groups:
 Arithmetic
 Relational
 Logical
 String
 Unary
ARITHMETIC OPERATORS

Operator Name Example $x=5; Result


$y=3
+ Addition $x + $y 8
- Subtraction $x - $y 2
* Multiplication $x * $y 15
/ Division $x / $y 1.67
(int) ($x/$y) 1
% Modulo $x % $y 2
** Exponentiation $x **$y 125
RELATIONAL OPERATORS

Operator Name Example $x=5; Result


$y=3
== Equality $x ==$y false
!=, <> Inequality $x !=$y true
> Greater Than $x > $y true
< Less Than $x < $y false
>= Greater than or $x >= $y true
Equal
<= Less than or equal $x <=$y false
LOGICAL OPERATORS

Operator Name Example Result


&&/and And $x &&$y True if both $x and
$y are true
||/or Or $x ||$y True if $x or $y are
true
! Not !$x True if $x is not
true
ASSIGNMENT OPERATORS

Assignment Sames as … Description


$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
 String Operators
Operator Name Example Result
. Concatenation $x . $y Concatenation
of $x and $y
.= Concatenation $x.= $y Appends $str2
assignment to $str1

Operator Name Example Result


++ Increment $x++; ++$x
 Unary Operators
-- Decrement $x--; --$x
CONTROL STRUCTURES
CONDITIONAL STATEMENTS
 In PHP we have the following conditional
statements:
 if statement - executes some code only if a specified
condition is true
 if...else statement - executes some code if a condition is
true and another code if the condition is false
 if...elseif....else statement - specifies a new condition to
test, if the first condition is false
 switch statement - selects one of many blocks of code to
be executed
IF STATEMENT
• Syntax
if (expression) {
// code to execute if the expression evaluates to true
}

<?php
$d1 = new DateTime("2013-12-09");
$d2 = new DateTime("2014-03-17");
$difference=$d1->diff($d2)->days;
if ($difference<100){
echo 100-$difference." left before 100 days";}
?>
IF..ELSE STATEMENT
• Syntax
if (expression) {
// code to execute if the expression evaluates to true
} else {
// code to execute in all other cases
}

<?php
$mood = "sad";
if ($mood == "happy") {
echo " Yehey, Masaya ako nasa mood ako";
}
else {
echo "Di Masaya kaya malungkot";
}
?>
IF..ELSEIF STATEMENT
• Syntax
if (expression) {
// code to execute if the expression evaluates to true
} else if (another expression) {
// code to execute if the previous expression failed
// and this one evaluates to true
} else {
// code to execute in all other cases
}

<?php
$mood = "sad";
if ($mood == "happy") {
echo "Yehey, Masaya ako nasa mood ako";
} elseif ($mood == "sad") {
echo "Di Masaya kaya malungkot”;
} else {
echo "Masaya b o malungkot?";
}
?>
SWITCH STATEMENT
• Syntax
switch (expression) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}

<?php
$mood = "sad";
switch ($mood) {
case "happy":
echo "Yehey, Masaya ako nansa mood ako";
break;
case "sad":
echo " Di Masaya kaya malungkot”;
break;
default:
echo "Masaya b o malungkot?";
break;
}
?>
LOOPING STATEMENT
WHILE STATEMENT
• Syntax
while (expression) {
// do something
}

<?php
$counter = 1;
$total=0;
while ($counter <= 12) {
echo "$counter times 2 is ".($counter * 2)."<br/>";
$total+=$counter*2;
$counter++;
}
echo “The total of the numbers is ”. $total;
?>
WHILE STATEMENT
• Syntax
while (expression) {
// do something
}

<?php
$counter = 1;
$total=0;
while ($counter <= 12) {
echo "$counter times 2 is ".($counter * 2)."<br/>";
$total+=$counter*2;
$counter++;
}
echo “The total of the numbers is ”. $total;
?>
DO..WHILE STATEMENT
• Syntax
do {
// do something
}while (expression);

<?php
$num = 1;
do {
echo "The number is: $num<br>";
$num++;
} while (($num > 200) && ($num < 400));?>
FOR..LOOP STATEMENT
• Syntax
for (initialization expression; test expression; modification
expression) {
// code to be executed
}

<?php
for ($counter=1; $counter<=12; $counter++) {
echo "$counter times 2 is ".($counter * 2)."<br/>";
}
?>
REFERENCES

 Gosselin, D. (2011). Php with MySQL.


 Pomperada, J. R. (2015). Php with MySQL : A
Web programming language. php.net
 tutorialrepublic.com/php-tutorial
 tutorialspoint.com/php/index.html
 w3schools.com/php
FORM HANDLING
$_GET PREDEFINED VARIABLE
 The $_GET variable is an array of variable names
and values sent by the HTTP GET method.
 The $_GET variable is used to collect values from a
form with method="get".
 Information sent from a form with the GET method is
visible to everyone (it will be displayed in the
browser's address bar) and it has limits on the amount
of information to send (max. 100 characters).

http://localhost/form.php?name=Peter&age=37
$_POST PREDEFINED VARIABLE
 The $_POST variable is an array of variable
names and values sent by the HTTP POST
method.
 The $_POST variable is used to collect values
from a form with method="post".
 Information sent from a form with the POST
method is invisible to others and has no limits
on the amount of information to send.
$_REQUEST PREDEFINED VARIABLE
 The PHP $_REQUEST variable contains the
contents of both GET, POST methods.
DEALING WITH MULTIVALUE FIELD
 Example
<input type= "checkbox" name=“number[]" value= "1" />1
<input type= "checkbox" name=“number[]" value= "2" />2
<input type= "checkbox" name=“number[]" value= "3" />3
STORING PHP VARIABLES IN FORMS
 A hidden field is a special type of input element
that can store and send a string value, however, it
is not displayed, its value cannot be change by
the user;
 Example
$name="John Smith";
<input type="hidden" name="name"
value="<?php echo $name?> " >
REDIRECTING AFTER A FORM SUBMISSION
 Example
header( “Location: thanks.html”);
header('Refresh: 10; URL=home.php');

You might also like