Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.

Slides:



Advertisements
Similar presentations
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
Copyright © Curt Hill PhP History and Introduction.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
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.
Pengantar Teknologi Internet W14: Server Scripting & Database.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
PHP Constructs Advance Database Management Systems Lab no.3.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python Functions.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
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.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Unit 10-JavaScript Functions Instructor: Brent Presley.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
FP512 WEB PROGRAMMING 1 PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
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 Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
 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,
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 7: User-Defined Functions II
PHP 5 Syntax.
PHP Introduction.
Author: truong toan thinh
Programmazione I a.a. 2017/2018.
User-Defined Functions
Topics Introduction to File Input and Output
Packages and Interfaces
PHP.
Lecture 5: Functions and Parameters
Intro to PHP.
Presentation transcript:

Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions

Creating FunctionstMyn2 Built-in functions PHP comes standard with many functions and constructs. There are also functions that require specific PHP extensions compiled in, otherwise fatal "undefined function" errors will appear. For example, to use image functions such as imagecreatetruecolor(), PHP must be compiled with GD support. Or, to use mysql_connect(), PHP must be compiled with MySQL support. There are many core functions that are included in every version of PHP, such as the string and variable functions. A call to phpinfo() or get_loaded_extensions() will show which extensions are loaded into PHP. Also note that many extensions are enabled by default and that the PHP manual is split up by extension.

Creating FunctionstMyn3 User-defined functions A function may be defined using syntax such as the following: function funktionName($formalParameter1, formalParameter2, …) { echo ” Example function. ”; … return $retValue; }

Creating FunctionstMyn4 Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. PHP does not support function overloading. Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.

Creating FunctionstMyn5 Give the function a name that reflects what the function does. The function name can start with a letter or underscore (not a number). Example of a simple function:

Creating FunctionstMyn6

Creating FunctionstMyn7

Creating FunctionstMyn8 Functions need not be defined before they are referenced:

Creating FunctionstMyn9

Creating FunctionstMyn10

Creating FunctionstMyn11 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. Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. PHP supports passing arguments by value (the default), passing by reference, and default argument values. Passing argument by value:

Creating FunctionstMyn12

Creating FunctionstMyn13

Creating FunctionstMyn14 The following function has two parameters:

Creating FunctionstMyn15

Creating FunctionstMyn16

Creating FunctionstMyn17 The include() statement includes and evaluates the specified file. The description below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a warning ( E_WARNING ), but require() results in a fatal error ( E_ERROR ). In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include path setting as well. Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script.

Creating FunctionstMyn18 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. However, all functions and classes defined in the included file have the global scope. If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.

Creating FunctionstMyn19 When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

Creating FunctionstMyn20

Creating FunctionstMyn21

Creating FunctionstMyn22

Creating FunctionstMyn23 Passing by reference By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference. To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.

Creating FunctionstMyn24 Note that there's no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.

Creating FunctionstMyn25

Creating FunctionstMyn26

Creating FunctionstMyn27

Creating FunctionstMyn28 After a minor change a reference parameter is in use:

Creating FunctionstMyn29

Creating FunctionstMyn30

Creating FunctionstMyn31 A function may define C++-style default values for scalar arguments as follows:

Creating FunctionstMyn32

Creating FunctionstMyn33

Creating FunctionstMyn34 Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example:

Creating FunctionstMyn35

Creating FunctionstMyn36

Creating FunctionstMyn37 This function is quite useless since every time it is called it sets $notGood to 0 and prints 0. The $notGood++ which increments the variable serves no purpose since as soon as the function exits the $notGood variable disappears. To make a useful counting function which will not lose track of the current count, the local variable is declared static :

Creating FunctionstMyn38

Creating FunctionstMyn39

Creating FunctionstMyn40 Now, $thisIsGood is initialized only in first call of function and every time the thisWorks() function is called it will print the value of $thisIsGood and increment it.

Creating FunctionstMyn41 Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so.

Creating FunctionstMyn42

Creating FunctionstMyn43

Creating FunctionstMyn44

Creating FunctionstMyn45 Next example utilizes the returned value in the calling part:

Creating FunctionstMyn46

Creating FunctionstMyn47

Creating FunctionstMyn48

Creating FunctionstMyn49

Creating FunctionstMyn50 A function can not return multiple values, but similar results can be obtained by returning an array.

Creating FunctionstMyn51

Creating FunctionstMyn52