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.

Slides:



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

Introduction to scripting
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.
Copyright © Curt Hill PhP History and Introduction.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
INTERNET APPLICATION DEVELOPMENT For More visit:
Nael Alian Introduction to PHP
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.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
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.
Introduction to Web Programming. Introduction to PHP What is PHP? What is a PHP File? What is MySQL? Why PHP? Where to Start?
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
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.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
PHP Variables.  Variables are "containers" for storing information. How to Declare PHP Variables  In PHP, a variable starts with the $ sign, followed.
 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.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
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.
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,
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.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
PHP using MySQL Database for Web Development (part II)
PHP Built-In Functions
Session 2 Basics of PHP.
A variable is a name for a value stored in memory.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
เอกสารประกอบการบรรยายรายวิชา Web Technology
PHP 5 Syntax.
Introduction to Web programming
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
PHP Rasmus Lerdorf.
Exploring JavaScript Ch 14
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Web Programming– UFCFB Lecture 19-20
Elements of the PHP Programming Environment
PHP Introduction.
Unit-1 Introduction to Java
PHP.
Variables Kevin Harville.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Variables In today’s lesson we will look at: what a variable is
Tutorial 6 PHP & MySQL Li Xu
Tutorial 10: Programming with javascript
Primitive Types and Expressions
PHP an introduction.
Use a variable to store a value that you want to use at a later time
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
JAVA. Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of platforms, such.
Presentation transcript:

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)

PHP Variables In PHP, a variable starts with the $ sign, followed by the name of the variable: <!DOCTYPE html> <html> <body> <?php $txt = "Hello world!"; $x = 5; $y = 10.5; echo $txt; echo "<br>"; echo $x; echo $y; ?> </body> </html> output : Hello world! 5 10.5

Output 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: <!DOCTYPE html> <html> <body> <?php $txt = "W3Schools.com"; echo "I love $txt!"; ?> </body> </html> Output : I love W3Schools.com!

PHP is a Loosely Typed Language In the example above, notice that we 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.

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 Scope A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: <!DOCTYPE html> <html> <body> <?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>"; ?> </body> </html> Output : Variable x inside function is: Variable x outside function is: 5

PHP The static Keyword A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function: <!DOCTYPE html> <html> <body> <?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>"; ?> </body> </html> Output : Variable x inside function is: 5 Variable x outside function is:

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: <!DOCTYPE html> <html> <body> <?php function myTest() { static $x = 0; echo $x; $x++; } myTest(); echo "<br>"; ?> </body> </html> Output : 0 1 2