Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.

Slides:



Advertisements
Similar presentations
Objectives Using functions to organize PHP code
Advertisements

PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Computer Science 103 Chapter 3 Introduction to JavaScript.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
CSCI 116 Functions.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Python.
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.
Intermediate PHP (2) File Input/Output & User Defined Functions.
PHP : Hypertext Preprocessor
INTERNET APPLICATION DEVELOPMENT For More visit:
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Why to Create a Procedure
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.
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.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
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.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
PHP Function. Outline o What Is Function ? o Create Function o Call Function o Parameters Functions o Function Returning Values o Passing by Reference.
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.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Python Let’s get started!.
 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
Functions Functions, locals, parameters, and separate compilation.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
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 First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
 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,
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Chapter 1.2 Introduction to C++ Programming
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
PHP 5 Syntax.
CHAPTER 5 SERVER SIDE SCRIPTING
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.
Elements of the PHP Programming Environment
PHP Introduction.
User-Defined Functions
Introduction to Web programming
Group Status Project Status.
PHP.
MCS/BCS.
Basics (Cont...).
COMPUTER PROGRAMMING SKILLS
PHP an introduction.
ITM 352 Functions.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value Pass Array To Function

What Is Function ?  In PHP we have tow type of functions : 1) User-defined Function. 2) Built-in Function.  User-defined Function : is the function created by user.  Built-in Function : is the function created by PHP, and ready to use.  The real power of PHP comes from its functions.  We just talk about user defined function in this chapter

User-Defined Function  User-defined function is just a name we give to a block of code that can be executed whenever we need it. This might not seem like that big of an idea, but believe me, when you understand and use functions you will be able to save a ton of time and write code that is much more readable!  A function will be executed by a call to the function.  You may call a function from anywhere within a page.  A function will be executed by a call to the function.  PHP function guidelines:  Give the function a name that reflects what the function does  The function name can start with a letter or underscore (not a number)

Create a PHP Function  Begins with keyword function and then the space and then the name of the function then parentheses”()” and then code block “{}” function functionName() { //code to be executed; }

Create a PHP Function Note: Your function name can start with a letter or underscore "_", but not a number! We want our function to print out the company motto each time it's called, so that sounds like it's a job for the echo command! <?php function myfunction() { echo “This is the first function to me "; } ?>

Call Function - Example <?php function myfunction() { echo “Muneer Masadeh"; } echo “my name is “; myfunction(); ?>

Parameters Functions To add more functionality to a function, we can add parameters. A parameter is just like a variable. Parameters are specified after the function name, inside the parentheses. <?php function myfunction($par1, $par2,……..) { echo “This is the first function with parameters to me "; } ?>

Parameters Functions <?php function myname($firstName) { echo “my name is ". $firstName ; } ?>

Parameters Functions – Call <?php function myname($firstName) { echo “my name is ". $firstName. "! "; } myname(“kalid"); myname("Ahmed"); myname(“laith"); myname(“muneer"); ?>

Parameters Functions - Call <?php function myname($firstName, $lastName) { echo "Hello there ". $firstName." ". $lastName."! "; } myname(“Kalid", “Ali"); myname("Ahmed", “Samer"); myname(“Wael", “Fadi"); myname(“Muneer", " Masadeh"); ?>

Parameters Functions - Call "; } echo "My name is "; writeName(“muneer ","."); echo " My family's name is "; writeName(" Masadeh ",“,"); echo “ I am Dr in "; writeName(“CS Department ",“!"); ?>

Function Returning Values  In addition to being able to pass functions information, you can also have them return a value. However, a function can only return one thing, although that thing can be any integer, float, array, string, etc. that you choose!  How does it return a value though? Well, when the function is used and finishes executing, it sort of changes from being a function name into being a value. To capture this value you can set a variable equal to the function. Something like: $myVar = somefunction();  Let's demonstrate this returning of a value by using a simple function that returns the sum of two integers.

Functions Returning Values – Example <?php function mySum($numX, $numY) { return ($numX + $numY); } $myNumber = 0; echo "Before call function, myNumber = ". $myNumber." "; // Store the result of mySum in $myNumber $myNumber = mySum(3, 4); echo "After call function, myNumber = ". $myNumber." "; ?>

Function Returning Values – Example <?php function factorial($number) { $ temp = 0; if($number <= 1) return 1; $temp = $number * factorial($number - 1); return $temp; } $ number = 4; if ($number < 0) echo "That is not a positive integer.\n"; else echo $number. " factorial is: ". factorial($number); ?>

Function Returning Values – Example <?php $n = 10; echo " The sum is “. sum($n) ; function sum($a) { if ( $n <= 0 ) return 0; else return ($n + sum($n-1)); } ?>

PHP Variable Scopes The scope of a variable is the part of the script where the variable can be referenced/used. PHP has four different variable scopes: local global static parameter

Local Scope A variable declared within a PHP function is local and can only be accessed within that function:

Local Scope The script above will not produce any output because the echo statement refers to the local scope variable $x, which has not been assigned a value within this 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. Local variables are deleted as soon as the function is completed.

Global Scope A variable that is defined outside of any function, has a global scope. Global variables can be accessed from any part of the script, EXCEPT from within a function. To access a global variable from within a function, use the global keyword:

Global Scope A variable that is defined outside of any function, has a global scope. Global variables can be accessed from any part of the script, EXCEPT from within a function. To access a global variable from within a function, use the global keyword:

Global Scope

Global Scope 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. The example above can be rewritten like this:

Global Scope

Static Scope When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted. To do this, use the static keyword when you first declare the variable:

Static Scope

Static Scope Then, each time the function is called, that variable will still have the information it contained from the last time the function was called. Note: The variable is still local to the function.

Parameter Scope A parameter is a local variable whose value is passed to the function by the calling code. Parameters are declared in a parameter list as part of the function declaration:

Passing Variable to a fonction You can pass a variable by Value to a function so the function can’t modify the variable. You can pass a variable by Reference to a function so the function can modify the variable.

Passing Variable By Value <?php $numX = 1; function byvalue ($numX) { $numX = $numX + 1; } byvalue ($numX); echo “the change after send data by value = ". $numX." "; ?>

Passing Variable By Reference <?php function byreference (&$numX) { $numX = $numX + 1; } byvalue ($numX); echo “the change after send data by Reference = ". $numX." "; ?>

Passing Variable By Reference

Passing Variable By Reference <?php function foo(&$var) { $var++; return $var; } function &bar() { $a = 5; return $a; } echo foo(bar()); ?>

Passing Variable By Reference

Example Chapter

Example <?php main(); function main() { $num1 = 10; $num2 = -6; $num3 = 2; $num4 = 1; $op = "-"; echo " "; echo " Expression is : $num1 $op $num2 $op $num3 $op $num4 = ".cal($num1, $num2, $num3, $num4,$op)." "; echo " Max number between ( $num1, $num2, $num3, $num4 ) = ".maxs($num1, $num2, $num3, $num4)." ";

Example echo " Min number between( $num1, $num2, $num3, $num4 ) = ".mins($num1, $num2, $num3, $num4)." "; echo " Positive numbers between( $num1, $num2, $num3, $num4 ) "; posi($num1, $num2, $num3, $num4); echo " "; echo " Negative numbers between( $num1, $num2, $num3, $num4 ) "; nega($num1, $num2, $num3, $num4); echo " "; }

Example function cal($num1, $num2, $num3, $num4,$op ) { switch($op) { case "+": return ($num1 + $num2+ $num3 + $num4 ); break; case "*": return ($num1 * $num2 * $num3 * $num4 ); break; case "/": return ($num1 / $num2 / $num3 / $num4 ); break; default : return ($num1 - $num2 - $num3 - $num4 ); break; }

Example function maxs($num1, $num2, $num3, $num4) { $max1 = $num1; if ($num2 > $max1) { $max1 = $num2; } if ($num3 > $max1) { $max1 = $num3; } if ($num4 > $max1) { $max1 = $num4; } return $max1; /* max is the largest value */ }

Example function mins($num1, $num2, $num3, $num4) { $min1 = $num1; if ($num2 < $min1) { $min1 = $num2; } if ($num3 < $min1) { $min1 = $num3; } if ($num4 < $min1) { $min1 = $num4; } return $min1; /* max is the largest value */ }

Example function posi($num1, $num2, $num3, $num4) { echo " "; $count = 0; if($num1 > 0) { echo " The $num1 is positive numbers "; $count++; } if($num2 > 0) { echo " The $num2 is positive numbers "; $count++; }

Example if($num3 > 0) { echo " The $num3 is positive numbers "; $count++; } if($num4 > 0) { echo " The $num4 is positive numbers "; $count++; } echo " The Total positive numbers is $count "; echo " "; }

Example function nega($num1, $num2, $num3, $num4) { $count = 0; echo " "; if($num1 < 0) { echo " The $num1 is negative numbers "; $count++; } if($num2 < 0) { echo " The $num2 is negative numbers "; $count++; }

Example if($num3 < 0) { echo " The $num3 is negative numbers "; $count++; } if($num4 < 0) { echo " The $num4 is negative numbers "; $count++; } echo " The Total negative numbers is $count "; echo " "; } ?>