PHP Function. Outline o What Is Function ? o Create Function o Call Function o Parameters Functions o Function Returning Values o Passing by Reference.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
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.
Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML
Computer Science 103 Chapter 3 Introduction to JavaScript.
CSCI 116 Functions.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
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.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
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.
Intermediate PHP (2) File Input/Output & User Defined Functions.
PHP : Hypertext Preprocessor
INTERNET APPLICATION DEVELOPMENT For More visit:
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Why to Create a Procedure
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
1 Procedures Blocks of code which can be called from anywhere in a program Enable us to avoid needless repetition of the same code Can take parameters.
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.
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.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
PHP Constructs Advance Database Management Systems Lab no.3.
CSC Intro. to Computing Lecture 12: PALGO. Announcements Homework #3 solutions available  Download from Blackboard/web Quiz #3 will be in class.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
PHP - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
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.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
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!.
Chapter 1 Introduction to PHP Part 1. Textbook’s Code DOWNLOADS PHP and MySQL for Dynamic Web Sites Complete Set of Scripts.
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
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.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
 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.
Chapter 1.2 Introduction to C++ Programming
Session 2 Basics of PHP.
CHAPTER 4 DECISIONS & LOOPS
Chapter 1.2 Introduction to C++ Programming
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Chapter 2 Basic Computation
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
PHP 5 Syntax.
CHAPTER 5 SERVER SIDE SCRIPTING
Introduction to Web programming
PHP Intro/Overview Bird Book pages 1-11,
Group Status Project Status.
PHP.
Chapter 4 Writing Classes.
Basics.
MCS/BCS.
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
PHP an introduction.
ITM 352 Functions.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP Function

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

What is function ? The real power of PHP comes from its functions.. A 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! In this chapter we will show you how to create your own functions. To keep the script from being executed when the page loads, you can put it into a function.

What is function ? 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 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 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.

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." "; ?>

Fonction 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); ?>

Fonction 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)); } ?>

Passing Variable to a function By Reference Vs Value  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

Examples <?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)." ";

Examples 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 " "; }

Examples 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; }

Examples 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 */ }

Examples 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 */ }

Examples 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++; }

Examples 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 " "; }

Examples 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++; }

Examples 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 " "; } ?>