11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
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.
PHP & MySQL Mahak Arora Vivek Bangera. Outline How PHP works Basic scripting in PHP Forms in PHP(GET & POST Variables) SQL basics PHP and MySQL connection.
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.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
INTERNET APPLICATION DEVELOPMENT For More visit:
PHP / MySQL. What is PHP? PHP is an acronym for "PHP Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed.
Nael Alian Introduction to PHP
Week 9 PHP Cookies and Session Introduction to JavaScript.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
PHP and JavaScript Nov. 26, 2013 Kyung Eun Park Computer and Creativity (COSC109) Towson University.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
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.
Introduction to Web Programming. Introduction to PHP What is PHP? What is a PHP File? What is MySQL? Why PHP? Where to Start?
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript Syntax, how to use it in a HTML document
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introduction to PHP.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
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.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
CHAPTER 6 Introduction to PHP5 Part I อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 A PHP script can be placed anywhere in the document.  A PHP script starts with  The default file extension for PHP files is ".php".  A PHP file normally.
Chapter 3 Introduction to PHP. Incorporating PHP Within HTML By default, PHP documents end with the extension.php files ending with.htm or.html to also.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
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.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
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,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
PHP using MySQL Database for Web Development (part II)
PHP 5 Syntax.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
PHP Variables A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume) Rules for PHP variables: A variable.
PHP Introduction.
Introduction to Web programming
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Web Programming Language
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University

 JavaScript generally runs in the browser, or client.  PHP runs on the same computer as the website you're visiting (server).  It has access to all the information and files on that machine, allows it to:  construct custom HTML pages to send to browser,  run tasks or perform calculations with data from that website.  etc.

 PHP is server side programming  need to install the "server" to run PHP code.  Easiest way to install Web server essentials:  Includes: Apache + MySQL + PHP + Perl

 By default, PHP documents end with the extension.php.  When a web server encounters this extension in a requested file, it automatically passes it to the PHP processor.  PHP code are executed on the server, and the result is returned to the browser as plain HTML

 PHP codes are placed between the tags  Example: My first PHP page

Two types of comments:  Single line comment // This is a comment  Multiline comment <?php /* This is a section of multiline comments that will not be interpreted */ ?>

 In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are NOT case-sensitive.  All three echo statements below are legal (and equal): "; echo "Hello World! "; EcHo "Hello World! "; ?>

 A semicolon after each statement is mandatory.  Without it, PHP treats multiple statements like one statement, which it is unable to understand.

 Place a $ in front of all variables  This is required to make the PHP parser faster, as it instantly knows whenever it comes across a variable.  Example: <?php $mycounter = 1; $mystring = "Hello"; $myarray = array("One", "Two", "Three"); echo $mycounter; echo " "; echo $mystring ; echo " "; echo $myarray[0] ; ?>

 Variable names must start with a letter of the alphabet or the _ (underscore) character.  Variable names can contain only the characters a-z, A-Z, 0-9, and _ (underscore).  Variable names may not contain spaces.  Variable names are case-sensitive.  PHP is a Loosely Typed Language  We do not have to tell PHP which data type the variable is.  PHP automatically converts the variable to the correct data type, depending on its value.

PHP has three different variable scopes:  Local Local variables are variables that are created within, and can be accessed only by a function. They are generally temporary variables that are used to store partially processed results prior to the function’s return.  Global There are cases when you need a variable to have global scope, because you want all your code to be able to access it. Also, some data may be large and complex, and you don’t want to keep passing it as arguments to functions.  Static Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable.

 Example: $x=5; // global scope function myTest() { $y=10; // local scope echo " Test variables inside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y"; } myTest(); echo " Test variables outside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y";

 The global keyword is used to access a global variable from within a function. $x=5; // global scope function myTest() { global $x; $y=10; // local scope echo " Test variables inside the function: "; echo "Variable x is: $x"; echo " "; echo "Variable y is: $y"; } myTest();

 Static variable example: function myTest() { static $x=0; echo $x; $x++; } myTest(); echo " "; myTest(); echo " "; myTest(); echo " "; myTest(); echo " "; myTest();

 Enclose each string in either double-quote or single quotes  String concatenation uses the period (. ) operator  Example: $msgs = 5; $name = "Manto\n"; $name2 = 'manto\n'; echo "$name"; echo '$name2'; echo "You have ". $msgs. " messages."; View source: \n goes here

 Two kinds of numeric variables:  Non-floating point(integer): $count=18;  Floating point: $speed=80.5;

 In PHP, the array() function is used to create an array.  There are three types of arrays:  Indexed arrays - Arrays with a numeric index  Associative arrays - Arrays with named keys  Multidimensional arrays - Arrays containing one or more arrays

Two ways to create indexed array:  Index can be assigned automatically: $cars=array("Volvo","BMW","Toyota");  Index can be assigned manually: $cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota";  Accessing an array: $cars=array("Volvo","BMW","Toyota"); echo "I like ". $cars[0]. ", ". $cars[1]. " and ". $cars[2]. ".";

 Associative arrays are arrays that use named keys  There are two ways to create an associative array: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); $age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43";  Accessing an Associative Array: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43") echo "Peter is ". $age['Peter']. " years old.";

 A multidimensional array is an array containing one or more arrays  Example: NameStockSold Volvo2218 BMW1513 Saab52 Land Rover1715 $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) );

Assignment Same as: $a += $b $a = $a + $b Addition $a -= $b $a = $a - $b Subtraction $a *= $b $a = $a * $b Multiplication $a /= $b $a = $a / $b Division $a %= $b $a = $a % $b Modulus $a.= $b $a = $a. $b String Concatenate $a &= $b $a = $a & $b Bitwise And $a |= $b $a = $a | $b Bitwise Or $a ^= $b $a = $a ^ $b Bitwise Xor $a >= $b $a = $a >> $b Right shift

In PHP we have the following conditional statements:  if statement - executes some code only if a specified condition is true  if...else statement - executes some code if a condition is true and another code if the condition is false  if...elseif....else statement - selects one of several blocks of code to be executed  switch statement - selects one of many blocks of code to be executed Very similar to Java !

 Example: $jam=date("H"); if($jam<20){ echo "Have a good day!"; } $jam=date("H"); if($jam<20){ echo "Have a good day!"; } else{ echo "Have a good night!"; }

 Example: $jam=date("H"); if($jam<10){ echo "Have a good morning!"; } elseif($jam<20){ echo "Have a good day!"; } else{ echo "Have a good night!"; }

 Example: $favcolor="red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, or green!"; }

In PHP we have the following conditional statements:  The while loop executes a block of code as long as the specified condition is true.  The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.  The for loop is used when you know in advance how many times the script should run.  The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Very similar to Java !

 Example: $x=1; while($x "; $x++; } $x=6; do{ echo "The number is: $x "; $x++; }while($x<=5); for ($x=0; $x "; }

 Example: $students=array("Anto","Toni","Budi","Wati"); foreach($students as $value){ echo $value." "; }

 A function is a block of statements that can be used repeatedly in a program.  A function will not execute immediately when a page loads.  A function will be executed by a call to the function.  A function name can start with a letter or underscore (not a number).  Function names are NOT case-sensitive.

 Example: function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function function writeMsg($name) { echo "Hello $name!"; } writeMsg("Budi"); // call the function

 We can add a default value for function’s parameter  Example: function setHeight($minheight=50) { echo "The height is : $minheight "; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80);

 Function can return a value  Example: function sum($x,$y) { $z=$x+$y; return $z; } echo " = ". sum(5,10). " "; echo " = ". sum(7,13). " "; echo "2 + 4 = ". sum(2,4);

 The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.  PHP array sort functions:  sort() - sort arrays in ascending order  rsort() - sort arrays in descending order  asort() - sort associative arrays in ascending order, according to the value  ksort() - sort associative arrays in ascending order, according to the key  arsort() - sort associative arrays in descending order, according to the value  krsort() - sort associative arrays in descending order, according to the key

 Example: $numbers=array(4,6,2,22,11); sort($numbers); $arrlength=count($numbers); for($x=0;$x "; } $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); asort($age); foreach($age as $x=>$x_value){ echo "Key=". $x. ", Value=". $x_value; echo " "; }