Você está na página 1de 14

PHP

(Hypertext Preprocessor)

Overview of PHP

PHP stands for Hypertext Preprocessor

Created by Rasmus Lerdorf in 1995

It is an open source software

It is server-side scripting language, like ASP and executed on the


server

More functionality added (OOP features), database support,


protocols and APIs

A language that combines elements of Perl, C, and Java

It is free to use and download

PHP files can contain text, HTML tags and scripts and file extension
of ".php" or ".phtml

PHP Syntax

PHP code is executed on the server, and the plain HTML result is
sent to the browser.

Syntax: It is starts with <?php and ends with ?>. A PHP scripting block can be
placed anywhere in the document.
<?php
?>

Comments in

<html>
<body>
PHP
<?php
//This is a comment

use // to make a single-line comment or /* and */ to make a large comment block.


/* This is a comment block
*/
?>
</body>
</html>

PHP Language Basics

Constants, Data Types and Variables

Constants define a string or numeric value

Constants do not begin with a dollar sign

Examples:

define(COMPANY, Acme Enterprises );

define(YELLOW, #FFFF00);

define(PI, 3.14);

define(NL, <br>\n);

Data types

Integers, doubles and strings

isValid = true; // Boolean

Variables

It is used to store information.


It is used for storing information, like text strings, numbers
or arrays.

$var_name
= value;
All variables
in PHP start
with a $ sign symbol.

The declaring a variable

<?php
$text=Welcome";
String variable:
echo $text;
?>

used to store and manipulate text

script assigns the text "Hello" to a string variable called $text:

Operators

Arithmetic Operators

Assignment Operators

+ (Addition), - (Subtraction), * (Multiplication),


++ (Increment),
-- (Decrement)

=, +=, -=, *=, /=, .=, %=

Comparison Operators

==,

!=,

<>,

>,

<,

>=,

<=

/ (Division),

% (Modulus ),

Conditional Statements

if Statement

execute some code only if a specified condition is true


if (condition) code to be executed if condition is true;

if (condition)
if...else Statement
code to be executed if condition is true;

else
code
to be
executed
if condition
false;
execute
some
code
if a condition
is trueisand
another code if the condition is false

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

if...elseif....else Statement

use this statement to select one of several blocks of code to be executed

Switch Statement

Conditional statements are used to perform different


actions based on different conditions.
Syntax:
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and
label2;
}

Loops

while Loop

executes a block of code while a condition is true.


while (condition)
{
code to be executed;
}

do
{
code to be executed;
}
execute
the block
while (condition);

do...while Statement

always
of code once, it will then check the
condition, and repeat the loop while the condition is true.

Loops (cont)

for Loop

The for loop is used when you know in advance how many times
for (init;
condition;
increment)
the
script
should
run.
{
code to be executed;
}

foreach ($array as $value)


{
code to be executed;
}

foreach Loop

The foreach loop is used to loop through arrays.

Form

One of the main features in PHP is the ability to take user


input and generate subsequent pages on the input. In this
page, we will introduce the mechanism by which data is
passes in PHP.
Let's consider
the following two files:
<form action=result.php type=post>

<input type=text name=employee>


query.php
<input type=submit value=Submit>
</form>

<?php
$employee_name=$_POST["employee"];
print $employee_name;
?>

result.php

$_GET Function

The built-in $_GET function is used to collect values in a form with


method="get".
When using method="get" in HTML forms, all variable names and
values are displayed in the URL.
<form
action=form.php"
method="get">
Information
sent
from a form
with the GET method is visible to
Name: <input type="text" name="name" />
everyone Age:
and<input
has type="text"
limits onname="age"
the amount
of information to send.
/>
<input type="submit" />
</form>

http://www.xyz.com/form.php?name=Denny&age=20

$_POST Function

The built-in $_POST function is used to collect values from a


form sent 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 and the variables are not displayed in
the URL and 8 Mb max size for the POST method, by default
also can be changed by setting the post_max_size in the
php.ini file.
Information
sent
from a form
with the POST method is
<form
action=form.php"
method="post">
<input and
type="text"
invisible toName:
others
hasname="name"
no limits/>on the amount of
Age: <input type="text" name="age" />
<input
/>
information
to type="submit"
send.
</form>

Thank You

Você também pode gostar