Elements of the PHP Programming Environment

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Storage class in C Topics Automatic variables External variables
Object Oriented Programming in PHP 5
Outline What Is Function ? Create Function Call Function Parameters Functions Function Returning Values PHP Variable Scopes Passing by Reference Vs Value.
PHP –Writing Reusable Code 10 March 2006 Adina Crainiceanu IT420: Database Management and Organization.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Liang Chapter 6 Variable scope and the keyword this.
IS 1181 IS 118 Introduction to Development Tools Chapter 5 Reusing Code.
The different kinds of variables in a Java program.
Storage Classes.
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 – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
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.
Scope of Variables. A variable can have something called a ‘scope’. This refers to its accessibility. Scope of Variable Mr.Skelton …Dalriada School Scope.
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.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
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.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
More PHP…. Understanding Variable Scope The term scope refers to the places within a script where a particular variable is visible. The six basic scope.
Week 11 Multi-file Programs and Scope of Variables.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
CHAPTER 6 Introduction to PHP5 Part I อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 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.
FP512 WEB PROGRAMMING 1 PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Object-Oriented PHP (2) Intermediate OO Concepts & Practice (1)
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,
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP using MySQL Database for Web Development (part II)
Introduction to Dynamic Web Programming
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Encapsulation, Data Hiding and Static Data Members
PHP 5 Syntax.
Storage class in C Topics Automatic variables External variables
PHP Rasmus Lerdorf.
Variable Scope Variable, variable, who’s got the variable?
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 Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer
Using local variable without initialization is an error.
Functions BIS1523 – Lecture 17.
More On Enumeration Types
PHP.
Chapter 8 Namespaces and Memory Realities
Local Variables, Global Variables and Variable Scope
SystemVerilog for Verification
Scope Rules and Storage Types
The lifespan of a variable
Objectives In this chapter, you will:
Tonga Institute of Higher Education
Intro to PHP.
Variables In today’s lesson we will look at: what a variable is
Tonga Institute of Higher Education
Tutorial 6 PHP & MySQL Li Xu
Welcome back to Software Development!
Submitted By : Veenu Saini Lecturer (IT)
PHP an introduction.
STORAGE CLASS.
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
STORAGE CLASS.
ITM 352 Functions.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Object Oriented Programming (OOP) Lecture No. 12
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Elements of the PHP Programming Environment PHP Variables Scope

PHP Variables Scope 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

Global and Local Scope A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: Example: <?php $x = 5; // global scope function myTest() { // using x inside this function will generate an error echo "<p>Variable x inside function is: $x</p>"; } myTest(); echo "<p>Variable x outside function is: $x</p>"; ?>

Output Variable x inside function is: Variable x outside function is: 5

LOCAL SCOPE A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function: <?php function myTest() { $x = 5; // local scope echo "<p>Variable x inside function is: $x</p>"; } myTest(); // using x outside the function will generate an error echo "<p>Variable x outside function is: $x</p>"; ?>

Output Variable x inside function is: 5 Variable x outside function is:

PHP The global Keyword 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):

Example <?php $x = 5; $y = 10; function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // outputs 15 ?>

PHP The static Keyword 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 <?php function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest(); ?>

Output 0 1 2 Note : Then, each time the function is called, that variable will still have the information it contained from the last time the function was called. Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.