Вы находитесь на странице: 1из 22

PHP Basics

Embedding PHP in HTML


Insert PHP tag inside HTML file (with .php extension) XML Style: <?php PHP statement; ?> Short Style (Need to be enabled):
<? PHP statement; ?>

Script Style:
<SCRIPT LANGUAGE='php'> PHP statement; </SCRIPT>

ASP Style (Need to be enabled):


<% PHP statement; %>

Comments
<html> <body> <?php //This is a comment echo "Normal Comment"; /* echo is a function that outputs something on the web page */ echo "Block comment"; # We shall talk about it later ?> </body>

Variables
Do not require to declare variable type; Simply assign a value and you can use the variable $name = Tom // this is string type $age = 20 // this is integer type Constants: define(PRICE', 100); There are two type of variables Local: can be accessed within a block Global: can be accessed throughout the file/system Local/Global these are called scopes

More about variables


Some convention when naming variables
They must start with an _ or alphabet character Must not start with a number Must not contain spaces (you can use _ i.e. underscore instead of spaces)

Operators
Pretty much same as in other programming languages (C, Java, etc.) Arithmetic operators: +,-,*,/, % etc Increment operators: ++, -- etc Assignment operators: =, +=, -=, *=, /= etc Comparison operator: ==, >=, <=, != Logical operators: &&, ||, !

Concat operator
PHP has a special operator . which is used as a concatenation operator for strings We will be relying on this operator most of the time More on this in the next sides

String manipulation
Let us check out the example:
<?php $txt1=Hello; $txt2= World; $txt3 = $txt1 . " " . $txt2 echo $txt3; ?>

This will output:


Hello World

We shall learn more string manipulation in function section

Conditional execution
If-else
Executes a block of code upon specific criteria
If(condition is true){ execute something }else{ execute something else }

IF-else
<html> <body> <?php $name = John; if ($name == John) // look clearfully the use of == echo Welcome John; else echo Who are you?; ?> </body> </html>

If-else if - else
<html> <body> <?php $name = John; if ($name == John) // look clearfully the use of == echo Welcome John; else if($name == Tom) echo Welcome Tom; else echo Who are you?; ?> </body> </html>

Array
Create an array (one dim array): $products = array ('Tires', 'Oil', 'Engine'); Automatically generate sequnces of number, character: $numbers = range (1,10,2) // range() is a function Accessing element: $products[0] Array with different indices (associative array): $prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );

Array (contd.)
Multidimensional Array: $products[row][column] $products = array( array( 'Code' => 'TIR, 'Description' => 'Tires, 'Price' => 100 ), array( 'Code' => 'OIL, 'Description' => 'Oil, 'Price' => 10 ), );

Loops
Looping means doing same task over time There are mainly thee kinds of looping is PHP
for while do ... while

For loop
For loop works as follows:
for (initialization; condition; increment) code to be executed; } Let us clarify this with an example:

For loop (contd.)


<html> <body> <?php for ($i=1; $i<=5; $i++){ echo "Hello World!<br />; } ?> </body> </html> This code will print Hello World five times

While Loop
In contrast to for loop, while loop has a slight different structure, although they both do the same work While loop works as follows: while (condition)
code to be executed;

Let us see another example

While loop (contd)


<html> <body> <?php $i = 1; while($i<=5){ echo "Hello World!<br />; $i++ // check where we are incrementing the value } ?> </body>

Function
New function: function my_function(){ echo 'My function was called; } Calling function my_function();

Function (contd.)
PHP has a lot of built in fuctions
Date(), Time() time/date function echo, sprintf(), strpos(), strtolower, strtoupper(), str_replace(), substr(), explode(), trim() string handling functions Fopen(), fclose() file handling functions

Creating our own function


Function my_function($n){ var $sum = 0; for($i=1;$i<=n;$i++){ $sum += $i; } return $sum; } What does this function do?

Superglobals $_GET and $_POST


There are some predefined variables in the server that are called superglbals You might recall $_GET and $_POST from last days lecture Let us make a simple application using this knowledge

Вам также может понравиться