Download presentation
Presentation is loading. Please wait.
1
Web Programming– UFCFB3-30-1 Lecture 19-20
Instructor : Mazhar H Malik Global College of Engineering and Technology
2
PHP Variables, Constants and Operators
Creating PHP Pages PHP Variables, Constants and Operators
3
Basic PHP Variables The values stored in computer memory are called variables The values, or data, contained in variables are classified into categories known as data types The name you assign to a variable is called an identifier
4
PHP Variables Prefixed with a $ Assign values with = operator Example: $name = “John Doe”; No need to define type Variable names are case sensitive $name and $Name are different
5
PHP Variables Variables are not statically typed
Integers can become floats can become strings Variable types include : Boolean Integer Float String Array Object Resource NULL
6
Assigned by reference, this links vars
PHP Variables Assigned by value $foo = "Bob"; $bar = $foo; Assigned by reference, this links vars $bar = &$foo; Some are preassigned, server and env vars For example, there are PHP vars, eg. PHP_SELF, HTTP_GET_VARS
7
Displaying Variables To display a variable with the echo statement, pass the variable name to the echo statement without enclosing it in quotation marks : $VotingAge = 18; echo $VotingAge; To display both text strings and variables, send them to the echo statement as individual arguments, separated by commas : echo "<p>The legal voting age is ", $VotingAge, ".</p>";
8
Naming Variables The name you assign to a variable is called an identifier The following rules and conventions must be followed when naming a variable: Identifiers must begin with a dollar sign ($) Identifiers may contain uppercase and lowercase letters, numbers, or underscores (_). The first character after the dollar sign must be a letter. Identifiers cannot contain spaces Identifiers are case sensitive
9
Declaring and Initializing Variables
Specifying and creating a variable name is called declaring the variable Assigning a first value to a variable is called initializing the variable In PHP, you must declare and initialize a variable in the same statement: $variable_name = value;
10
PHP Constants Constants are special variables that cannot be changed
Constant names do not begin with a dollar sign ($) Use them for named items that will not change Constant names use all uppercase letters Use the define() function to create a constant define("CONSTANT_NAME", value); The value you pass to the define() function can be a text string, number, or Boolean value
11
Standard Arithmetic operators String concatenation with a period (.)
PHP Operators Standard Arithmetic operators +, -, *, / and % (modulus) String concatenation with a period (.) $car = “SEAT” . “ Altea”; echo $car; would output “SEAT Altea” Basic Boolean comparison with “==” Using only = will overwrite a variable value Less than < and greater than > <= and >= as above but include equality
12
Assignment (=) and combined assignment
PHP Operators Assignment (=) and combined assignment $a = 3; $a += 5; // sets $a to 8; $b = "Hello "; $b .= "There!"; // sets $b to "Hello There!"; Bitwise (&, |, ^, ~, <<, >>) $a ^ $b(Xor: Bits that are set in $a or $b but not both are set.) ~ $a (Not: Bits that are set in $a are not set, and vice versa.)
13
Data Types A data type is the specific category of information that a variable contains Data types that can be assigned only a single value are called primitive types
14
Data Types PHP is not strictly typed
A data type is either text or numeric PHP decides what type a variable is PHP can use variables in an appropriate way automatically E.g. $vat_rate = 0.175; /* VAT Rate is numeric */ echo $vat_rate * “%”; //outputs “17.5%” $vat_rate is converted to a string for the purpose of the echo statement Object and Array also exist as types
15
Numerics Data Types PHP supports two numeric data types:
An integer is a positive or negative number and 0 with no decimal places (-250, 2, 100, 10,000) A floating-point number is a number that contains decimal places or that is written in exponential notation (-6.16, 3.17, ) Exponential notation, or scientific notation, is a shortened format for writing very large numbers or numbers with many decimal places (2.0e11)
16
Boolean Values A Boolean value is a value of TRUE or FALSE
It decides which part of a program should execute and which part should compare data In PHP programming, you can only use TRUE or FALSE Boolean values In other programming languages, you can use integers such as 1 = TRUE, 0 = FALSE
17
References References :
Anonymous.(n.d.). Apache HTTP Server Documentation Version 2.2. Retrieved from Achour, M., Betz, F. (n.d.), PHP Manual. Retrieved from Anonymous. (n.d.). MySQL Reference Manual. Retrieved from Naramore, E., Gerner, J., Le Scouarnec, Y., Stolz, J., Glass, M. K. (2005). Beginning PHP5, Apache, and MySQL® Web Development. Indianapolis, IN: Wiley Publishing, Inc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.