PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1.

Slides:



Advertisements
Similar presentations
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
Advertisements

1 Database Driven Web Application Clients Application Servers including web servers Database Server Traditional client-server (2-tier architecture): client:
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.
Lecture 4 Term 2 Introduction to PHP 30/1/12. Server Side Scripting This is a web server technology in which a user's request is fulfilled by running.
Copyright 2009 Justin C. Klein Keane PHP Code Auditing Session 1 – PHP Foundations Justin C. Klein Keane
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Php. What is PHP? PHP is an acronym for "PHP Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on.
WHAT IS PHP PHP is an HTML-embedded scripting language primarily used for dynamic Web applications.
PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).
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.
Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
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.
PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Nael Alian Introduction to PHP
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
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.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.
Introduction to CS520/CS596_026 Lecture Two Gordon Tian Fall 2015.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP Teresa Worner. What is it? PHP: Hypertext Preprocessor server-side scripting language open source cross-platform compatible with almost all servers.php.php3.phtml.
CS320 Web and Internet Programming Introduction to PHP Chengyu Sun California State University, Los Angeles.
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.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Introduction to PHP Advanced Database System Lab no.1.
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.
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.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Introduction to PHP.
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.
CHAPTER 6 Introduction to PHP5 Part I อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Intro to PHP More About PHP. So far we've covered... Web Servers & Protocols Presentation Layer (static pages) For the rest of the semester... Application.
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.
Learning PHP: Programming Review. PHP is one of many programming languages Different languages have their advantages and disadvantages and are therefore.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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,
PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
PHP WORKSHOP (Session 1) INFO 257 Supplement. Outline What is PHP ? PHP Language Basics PHP Exercise.
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.
PHP using MySQL Database for Web Development (part II)
Arrays An array in PHP is an ordered map
PHP (PHP: Hypertext Preprocessor)
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
WEB PROGRAMMING JavaScript.
PHP.
PHP an introduction.
Introduction to PHP.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP Basics 1 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1

Outline Introduction to PHP Comments Variables Type Type Casting Constants Operators Control Structures Misc. Functions 2

What is PHP? PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP. 3

There are three main areas where PHP scripts are used. Server-side scripting. Command line scripting. Writing desktop applications. The following databases are currently supported: IBM DB2, Informix, MySQL, ODBC, Oracle, PostgresSQL, SQLite, Sybase, etc., PHP Data Objects (PDO) LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM, SAX, XML, XSLT, etc., 4 What can PHP do?

Getting Started PHP is installed on the Apache HTTP Server (httpd) All files ending in.php are handled by PHP. e.g. index.php, helloworld.php Server parses files based on extensions Returns plain HTML, no code Think of these PHP-enabled files as simple HTML files with a whole new family of magical tags that let you do all sorts of things. 5

Open and close tags: <?php echo "Hello World!"; ?> 6 Hello World

Hello World (cont’d) Example <?php echo "Hello World!"; ?> 7 helloworld.php

<?php // $expression = TRUE; if ($expression) { ?> This is true. <?php } else { ?> This is false. <?php } ?> 8 escape.php Escaping from HTML

Comment <?php // one-line comment /* * multi line comment */ echo "Hello World!"; ?> 9

 In PHP, you create a variable with a dollar sign ($) and some text.  Usually the text will be something descriptive of what it is going to hold. <?php $name = " Web Programming " ; $code = ' ICS213 ' ; $credit = 3; $semester = " 1 / 2011 " ; ?> 10 Variable

Output one or more strings <?php echo ("Hello World"); echo "Hello World"; echo "This spans\nmultiple lines. The newlines will be\ noutput as well."; echo "Escaping characters is done \"Like this\"."; ?> 11 Echo, Print

Echo, Print (cont’d) <?php $foo = "foobar"; $bar = "barbaz"; echo "foo is $foo"; // foo is foobar echo 'foo is $foo'; // foo is $foo echo $foo; // foobar echo $foo,$bar; // foobarbarbaz echo "foo is ". $foo; // foo is foobar // preferred ?> 12 echo.php

13 Reserved Word

Booleans Integers Floating point numbers Strings Arrays Objects Resources NULL 14 Types

 It can be either TRUE or FALSE <?php $foo = TRUE; ?> 15 booleans.php Boolean

 An integer is a number of the set Z = {..., -2, -1, 0, 1, 2,...} <?php $int1 = 1234; // decimal number $int2 = -123; // a negative number $int3 = 0123; // octal number $int4 = 0x1A; // hexadecimal number ?> 16 Integer

 also known as "floats", "doubles", or "real numbers" 17 Float pointing numbers floats.php

 A string is series of characters <?php echo 'this is a simple string'; // Outputs: "I'll be back" echo '"I\'ll be back"'; // Outputs: Variables do not $expand $either echo 'Variables do not $expand $either'; ?> 18 String

. String catenation is indicated with a period (.) <?php $str1 = 'Hello'; // Outputs: Hello World PHP! echo "$str1 World". " PHP!"; ?> 19 string.php

 Simple array <?php $arr = array(1,2,3,4,5); echo $arr[3]; ?> 20 // 4 array1.php Array

Array (cont’d)  array( key => value,... ) // key may only be an integer or string // value may be any value of any type <?php $arr = array('foo' => 'bar', 12 => true); echo $arr['foo']; // bar echo $arr[12]; // 1 ?> 21 array2.php

Array (cont’d) <?php // This array is the same as... $arr1 = array(5 => 43, 32, 56, 'b' => 12); //...this array $arr2 = array(5 => 43, 6 => 32, 7 => 56, 'b' => 12); ?> 22 array3.php

 To create a new object, use the new statement to instantiate a class: <?php class Robot { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function __toString() { return $this->name; } $bender1 = new Robot(); $bender1->setName("Bender 1"); echo $bender1->__toString(); ?> 23 object.php Object

The special NULL value represents a variable with no value. <?php $var = NULL; ?> 24 null.php NULL

(int), (integer) - cast to integer (bool), (boolean) - cast to boolean (float), (double), (real) - cast to float (string) - cast to string (binary) - cast to binary string (PHP 6) (array) - cast to array (object) - cast to object (unset) - cast to NULL (PHP 5) 25 Type Casting

<?php $foo = 10; // $foo is an integer $str = "$foo"; // $str is a string $fst = (string) $foo; // $fst is also a string // This prints out that "they are the same" if ($fst === $str) { echo "they are the same"; } ?> 26 casting.php

Constant  A constant is case-sensitive by default <?php // a valid constant name define('HOST', 'localhost'); echo HOST; // localhost ?> 27 config.php

Arithmetic Operators Assignment Operator Comparison Operators Incrementing/Decrementing Operators Logical Operators String Operators Type Operators 28 Operators

PHP supports the usual operators supported by the C/C++/Java family 29 Arithmetic Operators

The assignment operator (=) used in C/C++/Java are supported in PHP 30 Assignment Operator

ExampleNameResult $a == $bEqualTRUE if $a is equal to $b. $a === $bIdentical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) $a != $bNot equalTRUE if $a is not equal to $b. $a <> $bNot equalTRUE if $a is not equal to $b. $a !== $bNot identical TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4) $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 >= $b Greater than or equal to TRUE if $a is greater than or equal to $b. 31 Comparison Operators

Incrementing/Decrementing Operators ExampleNameEffect ++$aPre-incrementIncrements $a by one, then returns $a. $a++Post-incrementReturns $a, then increments $a by one. --$aPre-decrementDecrements $a by one, then returns $a. $a--Post-decrementReturns $a, then decrements $a by one. 32

ExampleNameResult $a and $bAndTRUE if both $a and $b are TRUE. $a or $bOrTRUE if either $a or $b is TRUE. $a xor $bXorTRUE if either $a or $b is TRUE, but not both. ! $aNotTRUE if $a is not TRUE. $a && $bAndTRUE if both $a and $b are TRUE. $a || $bOrTRUE if either $a or $b is TRUE. 33 Logical Operators

ExampleNameResult $a = "Hello "; $b = $a. "World!"; concatenation operator“Hello World!” $a = "Hello "; $a.= "World!"; concatenating assignment operator “Hello World!” 34 String Operators

Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class: <?php class ParentClass { } class MyClass extends ParentClass { } $a = new MyClass; var_dump($a instanceof MyClass); // bool (true) var_dump($a instanceof ParentClass); // bool (true) ?> 35 type_op.php

If, else, else if while do-while for foreach break continue switch require include 36 Control Structures

<?php $a = 8; $b = 8; if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; } ?> 37 if

 while (expr) statement <?php $i = 1; while ($i < 10){ echo $i++; } ?> 38 while.php while

 while loops is guaranteed to run the first iteration of a do-while loop <?php $i = 0; do { echo $i; } while ($i > 0); ?> 39 dowhile.php do-while

 for (expr1; expr2; expr3) statement <?php for ($i = 1; $i <= 10; $i++) { echo $i; } ?> 40 for.php for

foreach  foreach (array_expression as $value) statement it loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element)  foreach (array_expression as $key => $value) statement The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop. 41

foreach (cont’d)  foreach (array_expression as $value) statement <?php $arr = array('one', 'two', 'three'); foreach ($arr as $value) { echo "Value: $value \n"; } ?> 42 foreach1.php

foreach (cont’d)  foreach (array_expression as $key => $value) statement <?php $arr = array('one', 'two', 'three'); foreach ($arr as $key => $value) { echo "Value: $value \n"; } ?> 43 foreach2.php

 break ends execution of the current for, foreach, while, do-while or switch structure. <?php $arr = array('one', 'two', 'stop', 'three'); while (list(, $val) = each($arr)) { if ($val == 'stop') { break; } echo "$val \n"; } ?> 44 break1.php break

break (cont’d) <?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; } ?> 45 break2.php

continue <?php /* The continue keyword can skip division by zero: */ $i = 5; while ($i > -2) { $i--; if ($i == 0) { continue; } echo $i. " "; } ?> 46 continue.php

switch <?php $i = 'orange'; switch ($i) { case 'apple': echo "i is apple"; break; case 'orange': echo "i is orange"; break; case 'cake': echo "i is cake"; break; } ?> 47 switch.php

 include() statement includes and evaluates the specified file. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward  include_once() // hi.php <?php echo "Hi, I'm a PHP script!"; ?> // world.php <?php include 'hi.php'; ?> 48 hi.php, world.php include

require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue. require_once() 49 hi.php, world2.php require

HTML 5 & CSS 3 50 template.php nav.php section.php footer.php header.php article.php <?php // template.php include_once 'header.php'; … include_once 'footer.php'; ?>

Misc. Functions constantReturns the value of a constant defineDefines a named constant. See Constant dieEquivalent to exit() exitOutput a message and terminate the current script highlight_fileSyntax highlighting of a file highlight_stringSyntax highlighting of a string 51 config,php, highlight_string.php Try config.phps

Reference PHP Manual,