1Computer Sciences Department Princess Nourah bint Abdulrahman University.

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
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.
LEARN THE QUICK AND EASY WAY! VISUAL QUICKPRO GUIDE Chapter 2: Programming with PHP Copyright © 2012 by Larry Ullman Dr. Mogeeb Mosleh Saturday ( pm)
INTERNET APPLICATION DEVELOPMENT For More visit:
Nael Alian Introduction to PHP
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Variables, Operators and Data Types. By Shyam Gurram.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
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.
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.
PHP Constructs Advance Database Management Systems Lab no.3.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Introduction to PHP.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
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.
PHP Variables.  Variables are "containers" for storing information. How to Declare PHP Variables  In PHP, a variable starts with the $ sign, followed.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
 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.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
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.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
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.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
 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.
PHP using MySQL Database for Web Development (part II)
Creating php Pages Done By: Hind Talafha.
PHP 5 Syntax.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Introduction to Web programming
PHP Rasmus Lerdorf.
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
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.
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

1Computer Sciences Department Princess Nourah bint Abdulrahman University

And use

PHP Part 2

 PHP 5 Syntax Objectives Computer Sciences Department4

 A PHP script is executed on the server, and the plain HTML result is sent back to the browser  A PHP script can be placed anywhere in the document.  A PHP script starts with :  Computer Sciences Department5 Introduction

 The default file extension for PHP files is ".php".  A PHP file normally contains HTML tags, and some PHP scripting code. Computer Sciences Department6

 A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.  Comments can be used to:  Let others understand what you are doing  Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code Computer Sciences Department7 Comments in PHP

Computer Sciences Department8

 In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive. Computer Sciences Department9 PHP Case Sensitivity

 all variable names are case-sensitive Computer Sciences Department10 But In the example, only the first statement will display the value of the $color variable (this is because $color, $COLOR, and $coLOR are treated as three different variables):

 In PHP, a variable starts with the $ sign, followed by the name of the variable:  After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value Note: When you assign a text value to a variable, put quotes around the value. Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. Computer Sciences Department11 PHP 5 Variables Creating (Declaring) 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 starts with the $ sign, followed by the name of the variable.  A variable name must start with a letter or the underscore character.  A variable name cannot start with a number.  A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).  Variable names are case-sensitive ($age and $AGE are two different variables) Computer Sciences Department12 PHP Variables

 The PHP echo statement is often used to output data to the screen.  The following example will show how to output text and a variable: Computer Sciences Department13 Output Variables

Computer Sciences Department14 Output the sum of two variables

 In the earlier examples, notice that the programmer did 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.  In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it. Computer Sciences Department15 PHP is a Loosely Typed Language

 In PHP, variables can be declared anywhere in the script.  The scope of a variable is the part of the script where the variable can be referenced/used.  PHP has three different variable scopes:  local  global  static Computer Sciences Department16 PHP Variables Scope

 variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function Computer Sciences Department17 Global Scope

 A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function Computer Sciences Department18 Local Scope

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared. Computer Sciences Department19 Note

How to use the global variable within a function????? Computer Sciences Department20

 The global keyword is used to access a global variable from within a function.  To do this, use the global keyword before the variables (inside the function) Computer Sciences Department21 PHP The global Keyword

 PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly Computer Sciences Department22 Olso  $GLOBALS[index].

 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  Then, each time the function is called, that variable will still have the information it contained from the last time the function was called Computer Sciences Department23 PHP The static Keyword

 In PHP there are two basic ways to get output: echo and print  echo and print are more or less the same. They are both used to output data to the screen.  The differences are small:  echo has no return value while print has a return value of 1 so it can be used in expressions.  echo can take multiple parameters (although such usage is rare) while print can take one argument.  echo is marginally faster than print. Computer Sciences Department24 PHP 5 echo and print Statements

 The echo statement can be used with or without parentheses: echo or echo().  Display Text  Display Variables Computer Sciences Department25 The PHP echo Statement

Computer Sciences Department26

 The print statement can be used with or without parentheses: print or print().  Display Text  Display Variables Computer Sciences Department27 The PHP print Statement

Computer Sciences Department28

 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer  Float (floating point numbers - also called double)  Boolean  Array  Object  NULL  Resource Computer Sciences Department29 PHP 5 Data Types

 A string is a sequence of characters, like "Hello world!".  A string can be any text inside quotes. You can use single or double quotes:  Example:  "; echo $y; ?> Computer Sciences Department30 PHP String

 An integer is a whole number (without decimals).  Rules for integers:  An integer must have at least one digit (0-9)  An integer cannot contain comma or blanks  An integer must not have a decimal point  An integer can be either positive or negative  Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)  In the following example $x is an integer. The PHP var_dump() function returns the data type and value: Computer Sciences Department31 PHP Integer

 A float (floating point number) is a number with a decimal point or a number in exponential form  Example  Computer Sciences Department32 PHP Float

 A Boolean represents two possible states: TRUE or FALSE  $x = true; $y = false;  Booleans are often used in conditional testing Computer Sciences Department33 PHP Boolean

 An array stores multiple values in one single variable.  In the following example $cars is an array.  The PHP var_dump() function returns the data type and value>  Example  Computer Sciences Department34 PHP Array

 An object is a data type which stores data and information on how to process that data.  In PHP, an object must be explicitly (CLEARLY) declared.  First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods. Computer Sciences Department35 PHP Object

 Null is a special data type which can have only one value: NULL.  A variable of data type NULL is a variable that has no value assigned to it.  Tip: If a variable is created without a value, it is automatically assigned a value of NULL Computer Sciences Department36 PHP NULL Value

 The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.  A common example of using the resource data type is a database call. Computer Sciences Department37 PHP Resource

 Get The Length of a String  The PHP strlen() function returns the length of a string (number of characters).  The example below returns the length of the string "Hello world!":  Example  Computer Sciences Department38 PHP String Functions

 Count The Number of Words in a String  The PHP str_word_count() function counts the number of words in a string:  Example  Computer Sciences Department39 PHP String Functions

 Reverse a String  The PHP strrev() function reverses a string:  Example  Computer Sciences Department40 PHP String Functions

 Search For a Specific Text Within a String  The PHP strpos() function searches for a specific text within a string.  If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE  Example  Computer Sciences Department41 PHP String Functions

 Replace Text Within a String  The PHP str_replace() function replaces some characters with some other characters in a string.  The example below replaces the text "world" with "Dolly":  Example  Computer Sciences Department42 PHP String Functions

 A constant is an identifier (name) for a simple value.  The value cannot be changed during the script.  A valid constant name starts with a letter or underscore (no $ sign before the constant name).  Note: Unlike variables, constants are automatically global across the entire script.  Create a PHP Constant  To create a constant, use the define() function.  Syntax: define(name, value, case-insensitive) Computer Sciences Department43 PHP Constants

 Parameters:  name: Specifies the name of the constant  value: Specifies the value of the constant  case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false Computer Sciences Department44 define(name, value, case-insensitive) case-insensitive

 Constants are automatically global and can be used across the entire script.  The example below uses a constant inside a function, even if it is defined outside the function:  Example  Computer Sciences Department45 Constants are Global

 Operators are used to perform operations on variables and values.  PHP divides the operators in the following groups:  Arithmetic operators  Assignment operators  Comparison operators  Increment/Decrement operators  Logical operators  String operators  Array operators Computer Sciences Department46 PHP 5 Operators

Computer Sciences Department47 PHP Arithmetic Operators

Computer Sciences Department48 PHP Assignment Operators

Computer Sciences Department49 PHP Comparison Operators

Computer Sciences Department50 PHP Increment / Decrement Operators

Computer Sciences Department51 PHP Logical Operators

Computer Sciences Department52 PHP Array Operators

 PHP Conditional Statements  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 - specifies a new condition to test, if the first condition is false  switch statement - selects one of many blocks of code to be executed Computer Sciences Department53 PHP 5 if...else...elseif Statements

 The if statement is used to execute some code only if a specified condition is true.  Syntax  if (condition) { code to be executed if condition is true; }  The example below will output "Have a good day!" if the current time (HOUR) is less than 20:  Example  Computer Sciences Department54 PHP - The if Statement

 Syntax  if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }  The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:  Example  Computer Sciences Department55 PHP - The if...else Statement

 Use the switch statement to select one of many blocks of code to be executed.  Syntax  switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break;... default: code to be executed if n is different from all labels; } Computer Sciences Department56 The PHP switch Statement Example

 PHP while loops execute a block of code while the specified condition is true. PHP Loops  Instead of adding several almost equal code-lines in a script, we can use loops to perform a task.  In PHP, we have the following looping statements:  while - loops through a block of code as long as the specified condition is true  do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true  for - loops through a block of code a specified number of times  foreach - loops through a block of code for each element in an array Computer Sciences Department57 PHP 5 while Loops

 The while loop executes a block of code as long as the specified condition is true.  Syntax  while (condition is true) { code to be executed; } Computer Sciences Department58 The PHP while Loop Example

 The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.  Syntax  foreach ($array as $value) { code to be executed; }  Example  "; } ?> Computer Sciences Department59 The PHP foreach Loop

 The real power of PHP comes from its functions; it has more than 1000 built-in 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 a program.  A function will not execute immediately when a page loads.  A function will be executed by a call to the function. Computer Sciences Department60 PHP 5 Functions

 A user defined function declaration starts with the word "function":  Syntax  function functionName() { code to be executed; }  Note:  A function name can start with a letter or underscore (not a number).  Function names are NOT case-sensitive Computer Sciences Department61 Create a User Defined Function in PHP Example

 Information can be passed to functions through arguments. An argument is just like a variable.  Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma.  Example  "; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); ?> Computer Sciences Department62 PHP Function Arguments

 The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:  Example  "; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?> Computer Sciences Department63 PHP Default Argument Value

 Example  "; echo " = ". sum(7, 13). " "; echo "2 + 4 = ". sum(2, 4); ?> Computer Sciences Department64 PHP Functions - Returning values

 An array stores multiple values in one single variable:  An array is a special variable, which can hold more than one value at a time.  Example  Computer Sciences Department65 PHP 5 Arrays

 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  Computer Sciences Department66 PHP 5 Sorting Arrays PHP - Sort Functions For Arrays