0% found this document useful (0 votes)
8 views

1 into php

This document serves as an introduction to PHP, covering its history, syntax, and various programming concepts such as variables, arrays, operators, and control structures. PHP is a server-side scripting language that is particularly effective for creating dynamic web content. The document also emphasizes the importance of understanding parsing, external data files, and the use of functions within PHP.

Uploaded by

complex web
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

1 into php

This document serves as an introduction to PHP, covering its history, syntax, and various programming concepts such as variables, arrays, operators, and control structures. PHP is a server-side scripting language that is particularly effective for creating dynamic web content. The document also emphasizes the importance of understanding parsing, external data files, and the use of functions within PHP.

Uploaded by

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

Introduction to php

PHP

Most of this is from the


PHP manual online at:
http://www.php.net/manual
/
What we'll cover
• A short history of php
• Parsing
• Variables
• Arrays
• Operators
• Functions
• Control Structures
• External Data Files
Background

• PHP is server side scripting


system
• PHP stands for "PHP: Hypertext
Preprocessor"
• Syntax based on Perl, Java, and C
• Very good for creating dynamic
content
• Powerful, but somewhat risky!
• If you want to focus on one system
for dynamic content, this is a good
one to choose
History
• Started as a Perl hack in 1994 by
Rasmus Lerdorf (to handle his
resume), developed to PHP/FI 2.0
• By 1997 up to PHP 3.0 with a new
parser engine by Zeev Suraski and
Andi Gutmans
• Version 7 is current version
• php is one of the premier
examples of what an open source
project can be
PHP Scripts
• Typically file ends in .php--this is set by
the web server configuration
• Separated in files with the <?php ?> tag
• php commands can make up an entire file, or
can be contained in html--this is a choice….
• Program lines end in ";" or you get an error
• Server recognizes embedded script and executes
• Result is passed to browser, source isn't
visible

<P>
<?php $myvar = "Hello World!";
echo $myvar;
?>
</P>
Parsing
• We've talk about how the browser can
read a text file and process it, that's
a basic parsing method
• Parsing involves acting on relevant
portions of a file and ignoring others
• Browsers parse web pages as they load
• Web servers with server side
technologies like php parse web pages as
they are being passed out to the browser
• Parsing does represent work, so there is
a cost
Two Ways

• You can embed sections of php


inside html:
<BODY>
<P>
<?php $myvar = "Hello World!";
echo $myvar;
</BODY>

• Or
<?php
you can call html from
php:
echo "<html><head><title>Howdy</title>

?>
What do we know already?
• Much of what we know about
javascript holds true in php (but
not all!), and other languages as
well
$name = "bil";
echo "Howdy, my name is
$name";
echo "What will $name be in
this line?";
echo 'What's wrong with this
line?';
if ($name == "bil")
{
// Hey, what's this?
Variables
• Typed by context (but one can
force type), so it's loose
• Begin with "$" (unlike
javascript!)
• Assigned by value
• $foo = "Bob"; $bar = $foo;

00
phpinfo()

• The phpinfo() function shows the


php environment
• Use this to read system and
server variables, setting stored
in php.ini, versions, and modules
• Notice that many of these data
are in arrays
• This is the first script you
should write…

00_phpinfo.ph
Variable Variables

• Using the value of a variable


as the name of a second
variable)
$a = "hello";
$$a = "world";
• Thus:
echo "$a ${$a}";
• Is the same as:
echo "$a $hello";

• But $$a echoes as "$hello"….


00_hello_world.ph
Operators
• Arithmetic (+, -, *, /, %) and
String (.)
• Assignment (=) and combined
assignment
$a = 3;
$a += 5; // sets $a to 8;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!";
• Bitwise (&, |, ^, ~, <<, >>)
• $a ^ $b (Xor: Bits that are set in $a or $b but
not both are set.)
• ~ $a (Not: Bits that are set in $a are not set, and
vice versa.)
• Comparison (==, ===, !=, !==, <, >,
<=, >=)
Operators: The Movie

• Incrementing/Decrementing

++$a (Increments by one, then returns $a.)



$a++ (Returns $a, then increments $a by one.)

--$(Decrements $a by one, then returns $a.)

$a--(Returns $a, then decrements $a by one.)
Son of the Valley of Operators
• Logical
$a and $b And True if both $a and $b are true.
$a or $b Or True if either $a or $b is true.
$a xor $b Xor True if either $a or $b is true,
but not both.
! $a Not True if $a is not true.
$a && $b And True if both $a and $b are true.
$a || $b Or True if either $a or $b is true.

• The two ands and ors have


different precedence rules,
"and" and "or" are lower
precedence than "&&" and "||"
• Use parentheses to resolve
precedence problems or just to
be clearer
Control Structures
• Wide Variety available
• if, else, elseif
• while, do-while
• for, foreach
• break, continue, switch
• require, include, require_once,
include_once
Control Structures
• Go and read about these
control structures

• if, elseif, else, while, for,


foreach, break and continue
Switch

• Switch, is very useful


• These two do the same
things…. switch ($i) {
case 0:
echo "i equals
if ($i == 0) { 0";
echo "i equals break;
0"; case 1:
echo "i equals
} elseif ($i == 1) {
1";
echo "i equals break;
1"; case 2:
} elseif ($i == 2) { echo "i equals
echo
example from "i equals 2";
http://us3.php.net/manual/en/control-structures.s
Nesting Files
• require(), include(), include_once(),
require_once() are used to bring in an external
file
• This lets you use the same chunk of code in a
number of pages, or read other kinds of files
into your program
• Be VERY careful of using these anywhere close to
user input--if a hacker can specify the file to
be included, that file will execute within your
script, with whatever rights your script has
Arrays
• You can create an array with the array function, or use the explode
function (this is very useful when reading files into web programs…)
$my_array = array(1, 2, 3, 4, 5);

$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";


$pieces = explode(" ", $pizza);

• An array is simply a variable representing a keyed list


• A list of values or variables
• If a variable, that var can also be an array
• Each variable in the list has a key
• The key can be a number or a text label
Arrays

• Arrays are lists, or lists of


lists, or list of lists of lists,
you get the idea--Arrays can be
multi-dimensional
• Array elements can be addressed by
either by number or by name
(strings)
Walking Arrays
• Use a loop, eg a foreach loop to
walk through an array
• while loops also work for arrays
with numeric keys--just set a
variable for the loop, and make
sure to increment that variable
within the loop
$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $color) {


echo "Do you like $color?\n";
}
05_arrays.ph
05_arrays.php
Array
• You can't echo an
(
array directly… [1] => Array
• You can walk (
through an echo or [sku] => A13412
[quantity] => 10
print() line by [item] => Whirly Wid
line [price] => .50
• You can use )
print_r(), this
will show you the [2] => Array
(
structure of [sku] => A43214
complex arrays-- [quantity] => 142
that output is to [item] => Widget Nut
the right, and it's [price] => .05
handy for learning )
the structure of an
Multidimensional Arrays
• A one dimensional array is a list, a spreadsheet
or other columnar data is two dimensional…
• Basically, you can make an array of arrays
$multiD = array
(
"fruits" => array("myfavorite" => "orange", "yuck" =>
"banana", "yum" => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
• The structure can be built array by array, or
declared with a single statement
• You can reference individual elements by nesting:
echo "<p>Yes, we have no " . $multiD["fruits"]["yuck"] . " (ok
by me).</p>";
• print_r() will show the entire structure, but
don’t forget the pre tags
Getting Data into arrays
• You can directly read data
into individual array slots
via a direct assignment:
$pieces[5] = "poulet
resistance";
Useful string functions

• str_replace()
• trim(), ltrim(), rtrim()
• implode(), explode()
• addslashes(), stripslashes()
• htmlentities(),
html_entity_decode(),
htmlspecialchars()
• striptags()
Sources

• http://www.zend.com/zend/
art/intro.php
• http://www.php.net/
• http://hotwired.lycos.com/
webmonkey/programming/php/
index.html

You might also like