Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Copy of PHP

No description
by

Mohamed Hussien

on 16 February 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Copy of PHP

PHP
References
Thank you for your attention!
PHP is a widely-used general-purpose
scripting language that is especially
suited for Web development and
can be embedded into HTML.
PHP is available for a wide variety of platforms, including UNIX, Microsoft Windows, Mac OS, and OS/2.

Additionally, because PHP code is interpreted and not compiled, PHP scripts written on one platform usually work as is on any platform for which an interpeter exists.
What is PHP?
PHP: An Interpreted Language
PHP's Portability
A variable is just a storage area.

You put things into your storage
areas (variables) so that you can
use and manipulate them in your programs.
Variables in PHP are
weakly-typed.

This means that you do not need to specify what type the variable is when you declare it.
This is how you declare a variable in PHP:
$variable_name = value;
$my_name = "Angelee";
$my_age = 19;
For example:
Variables in PHP are also dynamically-typed.

Dynamic typing generally means that the type of data an object can store can be changed; any target may hold a binding to any kind of object.
Variables
Example:
Dynamic typing:

$some_variable = "hello world";
$some_variable = 1;
$some_variable = new Object();
Arrays
An array is like a special variable,
which can hold
more than one value
at a time.
syntax: $variable_name = array();
$strings = array("string value", 1, new Object());
Declaring Arrays:
$strings = array();
$strings[] = "I"
$strings[] = "love"
$strings[] = "codes"
Associative
Arrays
It is an array where each ID key is
associated with a value.

With associative arrays we can use keys and assign values to them.
Declaring an Associative
Array:
$person_info = array("firstname" => "Angelee", "lastname" => "Pinzon");
$person_info = array();
$person_info['firstname'] = "Angelee";
$person_info['lastname'] = "Pinzon";
Accessing Array
Elements
syntax: $variable = $array_name[index];
Associative Arrays:
$firstname = $variable['firstname'];
$lastname = $variable['lastname'];
Since PHP variables are weakly-typed you can store different data types in a single array.
Numeric Arrays:
$zero = $numbers[0];
$one = $numbers[1];
You can assign values all at once:
Or even assign values one by one:
Assigning all at once:
Assigning one by one:
LOOPS
for($counter = 0; $counter < 10; $counter++) {
//statements here
}
For Loop
Syntax:
while (condition)
{
code to be executed;
}
While Loop
Do-While Loop
Syntax:
do {
//statements here
} while(condition);
ForEach Loop
It provides an easy way to iterate over arrays.
Foreach works only on arrays and objects, and will issue an
error when you try to use it on a variable with a
different data type or an uninitialized variable.
Syntax:
foreach (array_expression as $value) {
statement
}

foreach (array_expression as $key => $value) {
statement
}
$words = array("hello", "world", "hahaha");
Examples
foreach($words as $word) {
echo $word;
}
$words = array("first" => "hello", "second" => "world");
foreach($words as $order => $word) {
echo $order."=".$word;
}
In order to be able to directly
modify array elements within the
loop precede $value with &.

In that case the value will be
assigned by reference.
Handling
Form Data

The predefined $_GET variable is used to collect values in 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 has limits on the amount of information to send.
$_GET
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
$_GET Example:
When the user clicks the "Submit" button, the URL sent to the server could look something like this:
$_GET Example (Cont.)
http://www.w3schools.com/welcome.php?fname=Peter&age=37
$_GET Example (Cont.)
The "welcome.php" file can now use the $_GET
variable to collect form data (the names of the form fields will automatically be
the keys in the $_GET array):
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
The predefined $_POST variable 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.
$_POST
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
$_POST Example:
http://www.w3schools.com/
welcome.php
$_POST Example (Cont.)
When the user clicks the "Submit" button, the URL will look like this:
The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):
$_POST Example (Cont.)
Welcome <?php echo $_POST["fname"]; ?>!
<br />
You are <?php echo $_POST["age"]; ?> years old.
This method should not be used when sending passwords or other sensitive information!
When to use $_GET
However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.
Information sent from a form with the
POST method is invisible to others
and has no limits on the amount of information to send.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
When to use $_POST
Basic Syntax
A PHP script always starts
with <?php and ends with ?>.
A PHP script can be placed
anywhere in the document.
On servers with shorthand-
support,
you can start a
PHP script with <? and end
with ?>.
For maximum compatibility, we recommend that you use the
standard form (<?php) rather
than the shorthand form.
<?php ?>
A PHP file must have a .php
extension.

A PHP file normally contains
HTML tags, and some PHP scripting
code.
<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html>
<?php echo "hello World!"?>
Writing Comments

in PHP
In PHP, we use // to make a one-line
comment or /* and */ to make a
comment block:
<?php
//This is a comment.
/* This is a comment
block.*/
?>
Using PHP with
MySQL

What is MySQL?
MySQL is a database.
The data in MySQL is stored in database
objects called tables.
A table is a collection of
related data entries and it
consists of columns and
rows.
Connecting to MySQL
syntax: mysql_connect(servername,
username,password);
<?php
$connection = mysql_connect("localhost", "root", "password");

if(!$connection) {
die("No connection!");
} ?>
Closing a Connection
syntax: mysql_close($variable_holding_connection);
Example:
Example:
<?php
mysql_close($connection);
?>
PHP is an interpreted language.
You can run your code as is, without converting it first; the language interpreter reads and executes it.
What do I need
to run PHP Code?

Apache
The Apache web server intercepts HTTP requests and either serves them directly or passes them on to the PHP interpreter for execution.
PHP Interpreter
The PHP interpreter parses and executes PHP code, and returns the results to the web server.
MySQL
It serves as the data storage
engine, accepting connections
from the PHP Layer and inserting, modifying, or retrieving data.

www.w3schools.com
How to do everything with PHP and MySQL, Vikram Vaswani
The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
PHP uses the name attribute of each form element as a key in the $_GET or $_POST array.
Querying MySQL
The mysql_query() function executes a
query on a MySQL database.

This function returns the query handle for
SELECT queries, TRUE/FALSE for other
queries, or FALSE on failure.
syntax: mysql_query(query, connection)
Fetching Query Results From MySQL
mysql_fetch_row
mysql_fetch_array
Returns an associative array that corresponds to the fetched row and
moves the internal data pointer ahead.
mysql_fetch_assoc

Returns a numerical array
that corresponds to the fetched row and moves the internal data pointer ahead.
syntax: mysql_fetch_row (result )
syntax: mysql_fetch_array
(result [, result_type])
The type of returned array depends on how result_type is defined. By default, result_type is set to
MYSQL_BOTH
.
MYSQL_BOTH - you'll get an array with both associative and number indices.
MYSQL_ASSOC - you only get associative indices
MYSQL_NUM - you only get number indices
Values for result_type:
syntax: mysql_fetch_assoc (result )
www.php.net
Functions
A function is simply a set of
program statements which
perform a specific task, and
which can be “called”, or
executed, from anywhere
in your program.
What are functions?
Function Syntax
function function_name (optional function arguments) {

statement 1...

statement 2...
optional return return_value;
}
Example
function say_hi($name) {
echo "Hi, ".$name."!";
}
Output:
Name = "John":

Hi, John!
syntax: function_name(parameters);
Calling functions
example:

say_hi("Angelee");
will output:

"Hi Angelee!"
To add more functionality to a
function, we can add parameters.
A parameter is just like a variable.

Parameters are specified after the
function name, inside the
parentheses.
Function Parameters
function function_name(
parameter1...
)
You can also set default
values for the parameters.
Just make sure to place it last
to prevent unwanted behavior.
function say_hi($name="stranger") {
echo "Hi,".$name."!"
}
Only integer and string data
types are allowed to be used as
array keys.
Full transcript