PHP The Basic PHP. Out line o History of PHP o What is PHP? o Why PHP ? o What you need to start using PHP ? o What does PHP code look like? o Syntax.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling Learning XML Erik T. Ray CSCI 305 Introduction to Database.
Objectives Using functions to organize PHP code
Outline What is PHP? History of PHP Why PHP ? What is PHP file? What you need to start using PHP ? Syntax PHP code. echo & print Statement Variables.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
BY: ALBERTO CABEZAS 4/19/2010. INTRODUCTION: PHP is considered today as one of the most famous scripting languages. PHP is widely used as a general purpose.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP.
Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.
Introduction to PHP (Part-1) Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Server side basics.
PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1.
PHP The Basic. Outline  History of PHP  What is PHP?  What does PHP code look like?  Apache Server.  Syntax PHP code.  Anatomy of a PHP Script.
PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).
PHP PHP: Hypertext Preprocessing. What is PHP? PHP is a server-side scripting language designed specifically for the Web. An open source language PHP.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
ITD 3194 Web Application Development Chapter 4: Web Programming Language.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
AIT 616 Fall 2002 PHP. AIT 616 Fall 2002 PHP  Special scripting language used to dynamically generate web documents  Open source – Free!!!  Performs.
An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic.
Nael Alian Introduction to PHP
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Intro to PHP – Page 1 of 43CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Intro to PHP Reading: Chapters 1.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
PHP By Sergio Rodriguez By Sergio Rodriguez. PHP G PHP: Hypertext Preprocessor G Scripting language G PHP: Hypertext Preprocessor G Scripting language.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
PHP OVERVIEW. C ONTENT Introduction of PHP History and development Basics of PHP programming.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
WEB DEVELOPMENT WITH PHP/MYSQL. WEB DEVELOPMENT COURSE HAS DIFFERENT NAME IN DIFFERENT INSITUTES, THIS IS A CORE COURSE FOR BS/MS STUDENTS. THIS IS ALSO.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introduction to PHP.
JavaScript, Fourth Edition
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
CSE 154 LECTURE 14: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
By bscshelp.com 1.  It is a group assignment.  Complete Website design Using Html and Css.  Due date: 10 th December, 2014 (Hard Deadline) 2.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
PHP using MySQL Database for Web Development (part II)
CHAPTER 5 SERVER SIDE SCRIPTING
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be.
PHP (PHP: Hypertext Preprocessor)
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be.
PHP Introduction.
PHP.
Basics.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Intro to PHP.
PHP an introduction.
23 PHP.
Presentation transcript:

PHP The Basic PHP

Out line o History of PHP o What is PHP? o Why PHP ? o What you need to start using PHP ? o What does PHP code look like? o Syntax PHP code. o Echo Statement o Variables. o Anatomy of a PHP Script. o Data Types. o Constants &Operators. o Control Structures. o Errors and Error Management.

History of PHP PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in It was initially developed for HTTP usage logging and server-side form generation in Unix. PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. PHP 3 (1998) added support for ODBC data sources, multiple platform support, protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans. PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP

What is PHP?  Personal Homepage Tools/Form Interpreter  PHP is a Server-side Scripting Language designed specifically for the Web.  An open source language  PHP code can be embedded within an HTML page, which will be executed each time that page is visited.  Filenames end with.php by convention

What is PHP? (cont’d) Interpreted language, scripts are parsed at run-time rather than compiled beforehand Executed on the server-side Source-code not visible by client ‘View Source’ in browsers does not display the PHP code Various built-in functions allow for fast development Compatible with many popular databases

Why PHP ? Open source / free software Cross platform to develop and deploy and to use Powerful, robust, scalable Web development specific Can be object oriented especially version 5 Large active developer community (20 millions websites Great documentation in many language

What you need to start using php ? Installation You will need 1.Web server ( Apache ) 2.PHP ( version 5.3) 3.Database ( MySQL 5 ) 4.Text editor (Notepad) 5.Web browser (Firefox ) 6. en/install.phpwww.php.net/manual/ en/install.php

What does PHP code look like? Structurally similar to C/C++ Supports procedural and object-oriented paradigm (to some degree) All PHP statements end with a semi-colon Each PHP script must be enclosed in the reserved PHP tag <?php … ?>

Syntax PHP code Standard Style : Short Style: Script Style: ASP Style:

Echo The PHP command ‘echo’ is used to output the parameters passed to it. The typical usage for this is to send data to the client’s web- browser void echo (string arg1 [, string argn...]) Syntax : void echo (string arg1 [, string argn...]) In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function

Echo - Example <?php echo “ This my first statement in PHP language“; ?>

Variables PHP variables must begin with a “$” sign Case-sensitive ($Foo != $foo != $fOo) Global and locally-scoped variables Global variables can be used anywhere Local variables restricted to a function or class Certain variable names reserved by PHP Form variables ($_POST, $_GET) Server variables ($_SERVER)

Variables <?php $name = “ali” echo( $name); ?>

Variables <?php $name = “Mohamed”; $age = 23; Echo “ My name is $name and I am $age years old”; ?>

Variables <?php $name = 'elijah'; $yearborn = 1975; $currentyear = 2005; $age = $currentyear - $yearborn; echo ("$name is $age years old."); ?>

Variables A simple PHP document Welcome to PHP, !

Single & Double Quotes <?php echo “ Hello world ”; echo ‘ Hello world’; ?>

<?php $word = ‘ World’; echo “ Hello $word ”; echo ‘ Hello $word ’; ?> Single & Double Quotes

Anatomy of a PHP Script // or # for single line /* */ for multiline /* this is my comment one this is my comment two this is my comment three */  Comments

Anatomy of a PHP Script You cant have any whitespace between <? and php. You cant break apart keywords (e.g :whi le,func tion,fo r) You cant break apart varible names and function names (e.g:$var name,function f 2)  Whitespace

Anatomy of a PHP Script Is simply a series of statements' enclosed between two braces : { //some comand }  Code Block

Concatenation Use a period to join strings into one. <?php $string1=“Hello”; $string2=“PHP”; $string3=$string1. “ ”. $string2; Print $string3; ?> <?php $string1=“Hello”; $string2=“PHP”; $string3=$string1. “ ”. $string2; Print $string3; ?> Hello PHP

Escaping the Character If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks to ignore and display them. <?php $heading=“\”Computer Science\””; Science”; echo $heading; echo $heading1; ?> <?php $heading=“\”Computer Science\””; Science”; echo $heading; echo $heading1; ?> “Computer Science”

Example Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25 Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP This is true for both variables and character escape-sequences (such as “\n” or “\\”) <?php $foo = 25;// Numerical variable $bar = “Hello”;// String variable echo $bar;// Outputs Hello echo $foo,$bar;// Outputs 25Hello echo “5x5=”,$foo;// Outputs 5x5=25 echo “5x5=$foo”;// Outputs 5x5=25 echo ‘5x5=$foo’;// Outputs 5x5=$foo ?>

Data type

<?php // declare a string, double and integer $testString = "3.5 seconds"; $testDouble = 79.2; $testInteger = 12; print( $testString ).”is a string ”; print( $testDouble ).”is a double ” print( $testInteger ).”is an integer ”; ?>

Data type <?php // call function settype to convert variable // testString to different data types print( "$testString" ); settype( $testString, "double" ); print( " as a double is $testString " ); print( "$testString" ); settype( $testString, "integer" ); print( " as an integer is $testString " ); settype( $testString, "string" ); print( "Converting back to a string results in $testString " ); ?>

Data type <?php $data = "98.6 degrees"; echo( "Now using type casting instead: As a string - ". (string) $data. " As a double - ". (double) $data." As an integer - ". (integer) $data ); ?>

Data type <?php $a = “ 12.4 abc” echo (int) $a; echo (double) ($a); echo (float) ($a); echo (string) ($a); ?>

Arithmetic Operations - Assignment operators Syntactical shortcuts Before being assigned values, variables have value undef - Constants Named values define function

Arithmetic Operators $a - $b // subtraction $a * $b// multiplication $a / $b// division $a += 5// $a = $a+5 Also works for *= and /= <?php $a=15; $b=30; $total=$a+$b; echo $total; echo“ $total ”; // total is 45 ?>

Arithmetic Operators <?php $a =1; echo $a++; // output 1,$a is now equal to 2 echo ++$a; // output 3,$a is now equal to 3 echo --$a; // output 2,$a is now equal to 2 echo $a--; // output 2,$a is now equal to 1 ?>

Arithmetic Operators <?php $a =(int)(‘test’); // $a==0 echo ++$a; ?>

Math operations <?php $num1 = 10; $num2 =20; // addition echo $num1+$mum2. ‘ ’; //subtraction echo $num1 - $num2. ‘ ’; // multiplication ?>

<?php // Multiplication echo $num1* $num2. ‘ ’; // Division Echo $num1/num2. ‘ ’ ; //increment $num1++; $Num2--; Echo $num1; ?> Math operations

Example <?php $a = 5; print( "The value of variable a is $a " ); // define constant VALUE define( "VALUE", 5 ); // add constant VALUE to variable $a $a = $a + VALUE; print( "Variable a after adding constant VALUE is $a " );

Example cont.. // multiply variable $a by 2 $a *= 2; print( "Multiplying variable a by 2 yields $a " ); // test if variable $a is less than 50 if ( $a < 50 ) print( "Variable a is less than 50 " ); // add 40 to variable $a $a += 40; print( "Variable a after adding 40 is $a " ); // test if variable $a is 50 or less if ( $a < 51 ) print( "Variable a is still 50 or less " ); // test if variable $a is between 50 and 100, inclusive elseif ( $a < 101 ) print( "Variable a is now between 50 and 100, inclusive " ); else print( "Variable a is now greater than 100 " );

Example cont.. // print an uninitialized variable print( "Using a variable before initializing: $nothing " ); // add constant VALUE to an uninitialized variable $test = $num + VALUE; print( "An uninitialized variable plus constant VALUE yields $test " ); // add a string to an integer $str = "3 dollars"; $a += $str; print( "Adding a string to variable a yields $a " ); ?>

Logic Operations ExampleNameResult $a == $bEqualTRUE if $a is equal to $b after type juggling. $a === $bIdenticalTRUE if $a is equal to $b, and they are of the same type. $a != $bNot equalTRUE if $a is not equal to $b after type juggling. $a <> $bNot equalTRUE if $a is not equal to $b after type juggling. $a !== $bNot identicalTRUE if $a is not equal to $b, or they are not of the same type. $a < $bLess thanTRUE if $a is strictly less than $b. $a > $bGreater thanTRUE if $a is strictly greater than $b. $a <= $bLess than or equal toTRUE if $a is less than or equal to $b. $a >= $bGreater than or equal toTRUE if $a is greater than or equal to $b.

Bitwise Operations ExampleNameResult A & BAnd Bits that are set in both A and B are set. A | BOr Bits that are set in either A or B are set. A ^ BXor Bits that are set in A or B but not both are set. ~ ANot Bits that are set in A are not set, and vice versa. A << BShift left Shift the bits of A B steps to the left (each step means "multiply by two") A >> BShift right Shift the bits of A B steps to the right (each step means "divide by two")

Example cont.. <?php $x=13; $y=22; echo $x & $y; ?> <?php $x=77; $y=198; echo $x & $y; ?>

<?php $x=5; $y=11; echo $x | $y; ?> <?php $x=12; $y=11; echo $x ^ $y; ?> Example cont..

<?php $x=12; $y=10; echo $x & ~ $y; ?> <?php $x=8; $y=3; echo $x << $y; ? Example cont..

<?php $x=12; $y=10; echo $x & ~ $y; ?> <?php $x=8; $y=3; echo $x << $y; ? Example cont..

<?php $x=12; $y=4; echo $x << $y; ?> <?php $x=8; $y=3; echo $x >> $y; ?> Example cont..

Referencing Operators We know the assignment operators work by value,by copy the value to other expression,if the value in right hand change the value in left is not change. Ex: <?php $a =10; $b =$a; $b =20 Echo $a; // 10 ?>

Referencing Operators But we can change the value of variable $a by the reference, that mena connect right hand to left hand, Example: <?php $a =10; $b = &$a; $b= 20; echo $a; // 20 ?>

Control Structures  Control Structures: Are the structures within a language that allow us to control the flow of execution through a program or script.  Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops). 

If Statement if (condition) { statements; } else { statement; } <?php $user = “jone”; if($user==“john”) { print “hello john.”; } else { print “you are not john.”; } ?> hello john

if/else if/else statement <?php if ($foo == 0) { echo ‘The variable foo is equal to 0’; } else if (($foo > 0) && ($foo <= 5)) { echo ‘The variable foo is between 1 and 5’; } else { echo ‘The variable foo is equal to ‘.$foo; } ?>

Switch Statment switch(expression ) { case value: break;. default: break; } <?php $count=0; switch($count) { case 0: Print “hello PHP3. ”; break; case 1: Print “hello PHP4. ”; break; default: Print “hello PHP5. ”; break; ?> hello PHP3

Switch - Example <?php $total = 0; $i = 2; switch($i) { case 6: $total = 99; break; case 1: $total += 1;break; case 2:$total += 2;break; case 3: $total += 3; ;break; case 4:$total += 4; break; default : $total += 5;break; } echo $total; ?>

For Loop for ($varible = value ;condition;$value assigment ) { statements; } <?php $count=0; for($count = 0;$count <3,$count++) { Print “hello PHP. ”; } ?> hello PHP. hello PHP. hello PHP.

For - Example <?php for ($i = 1; $i <= 10; $i++) { echo $i; } ?>

For - Example <?php for ($i = 1, $j = 0; $i <= 10; $j += $i, echo $i, $i++); ?>

For-Example <?php $brush_price = 5; echo " "; echo " Quantity "; echo " Price "; for ( $counter = 10; $counter <= 100; $counter += 10) { echo " "; echo $counter; echo " "; echo $brush_price * $counter; echo " "; } echo " "; ?>

While Loop while (condition) { statements; } <?php $count=0; while($count<3) { echo “hello PHP. ”; $count += 1; // $count = $count + 1; // or // $count++; } ?> hello PHP. hello PHP. hello PHP.

While - Example <?php $i = 0; while ($i++ < 5) { echo “loop number : “.$i; } ?>

Do... While Loop do { statements; } while (condition); <?php $count=0; do { echo “hello PHP. ”; $count += 1; // $count = $count + 1; // or // $count++; } while($count<3); ?> hello PHP. hello PHP. hello PHP.

Do..While 0); ?>

Errors & Error Management

Isset Function bool isset ( $var ) Determine if a variable is set and is not NULL. If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULLbyte ("\0") is not equivalent to the PHP NULL constant. If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

Isset Function <?php $var = ''; // This will evaluate to TRUE so the text will be printed. if (isset($var)) { echo "This var is set so I will print."; } ?>

Unset Function void unset ( $var) unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

unset Function <?php $foo = 'bar'; echo $foo; unset($foo); echo $foo; ?>

Info PHP Page <?php phpinfo(); ?>

For..If '; for($i = 0; $i '. $i. ' '; if(($i + 1) % $rows == 0){ echo ' '; } } echo ' '; ?>

For "; } ?>

Nested For

While - Switch <?php $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5 \n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting \n"; break 2; /* Exit the switch and the while. */ default: break; } } ?>

Continue

If - Switch <?php $i = 1; if ($i == 0) { echo "i equals 0"; } elseif ($i == 1) { echo "i equals 1"; } elseif ($i == 2) { echo "i equals 2"; } switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } ?>

Do..While - IF

If in other style

Goto