Download presentation
Presentation is loading. Please wait.
Published byHorace Hall Modified over 9 years ago
1
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics
2
Open Source Server Side Scripting 2 ECA 236 PHP Browser Server PHP.php Request Response Three types of errors can occur: 1.Parse 2.Execution 3.Logic
3
Open Source Server Side Scripting 3 ECA 236 4 different styles of PHP tags: XML style Short style Script style ASP style Basic Syntax
4
Open Source Server Side Scripting 4 ECA 236 Basic Syntax cont … Files end with the extension.php All statements end with a semicolon White space is ignored 3 styles of comments: # comments // comments /* comments */
5
Open Source Server Side Scripting 5 ECA 236 Basic Syntax cont … All class assignments must contain, in comments: Name Date Name of Assignment Purpose of Assignment /*Michael Barath 3 September 2003 Lab #3 Use loops and conditional to create and format a table */
6
Open Source Server Side Scripting 6 ECA 236 Variables Naming Rules: prefixed with a $ begin with letter or underscore may contain only letters, number, underscores case sensitive Assign a value with the assignment operator ( = ) Do not have to be declared before use $first_name = “Michael”;
7
Open Source Server Side Scripting 7 ECA 236 Variables cont … Variable Types strings integers floating-point Boolean array object resources – special variable, holding a reference to an external resource such as an open file, database connection, or image canvas NULL – a variable that has no value
8
Open Source Server Side Scripting 8 ECA 236 Sending data to browser Built-in functions to send data to browser: echo( ) allows you to send one or multiple pieces of code, separated by commas $first_name = “Michael”; $greeting = “How are you?”; echo “Hello, $first_name. $greeting”; echo “Hello, ”, $first_name, “.”, $greeting;
9
Open Source Server Side Scripting 9 ECA 236 Sending data to browser cont … Built-in functions to send data to browser: print( ) returns a value of TRUE or FALSE, if the function was called successfully Technically neither echo( ) nor print( ) is a true function, but a language construct $first_name = “Michael”; $greeting = “How are you?”; print “Hello, $first_name. $greeting”;
10
Open Source Server Side Scripting 10 ECA 236 Strings Blocks of textual information Double Quotes interpolated variables $name = “Michael”; echo “My name is $name”; // prints “My name is Michael”
11
Open Source Server Side Scripting 11 ECA 236 Strings cont … Single Quotes literals $name = “Michael”; echo ‘My name is $name’; // prints “My name is $name”
12
Open Source Server Side Scripting 12 ECA 236 Strings cont … Use \ to escape quotes, $, \, etc $name = “Michael”; echo “$name said, \”Bob owes me \$20.\” ”; // prints Michael said, “Bob owes me $20.” echo ‘Michael\’s reply was “What, me worry?” ’; // prints Michael’s reply was “What, me worry?”
13
Open Source Server Side Scripting 13 ECA 236 Strings cont … Concatenation Operator dot, or period $first_name = “Jacques”; $last_name = “Renault”; echo $whole_name = $first_name. $last_name; // prints JacquesRenault echo $whole_name = $first_name. “ “. $last_name; // prints Jacques Renault
14
Open Source Server Side Scripting 14 ECA 236 Strings cont … newlines and tabs \n \t adds returns or tabs to the way code prints, not the way a document renders in the browser echo “ My Title ”; // prints My Title
15
Open Source Server Side Scripting 15 ECA 236 Strings cont … newlines and tabs cont … echo “ \n \n\t My Title \n ”; /* prints My Title */
16
Open Source Server Side Scripting 16 ECA 236 Strings cont … newlines and tabs cont … echo “ ”; echo “\t My Title ”; echo “ ”; /* prints My Title */
17
Open Source Server Side Scripting 17 ECA 236 Numbers integer and floating point Standard arithmetic operators: +addition –subtraction *multiplication /division $x = 7; $y = 3; $z = $x * $y ; // 21
18
Open Source Server Side Scripting 18 ECA 236 Numbers cont … Standard arithmetic operators: %modulus remainder, after division has occurred $x = 7; $y = 3; $z = $x % $y ; // 1
19
Open Source Server Side Scripting 19 ECA 236 Numbers cont … Standard arithmetic operators: + +increment – –decrement $x = 7; echo $x++ ; // prints 7 echo ++$x ; // prints 8 $x = 7; echo $x- - ; // prints 7 echo - - $x ; // prints 6
20
Open Source Server Side Scripting 20 ECA 236 Numbers cont … PHP includes dozens of functions to use with numbers. EG: round( ) – rounds a decimal to the nearest integer takes one or two arguments number to be rounded number of decimal places $x = 313.137687; $x = round( $ ); // 313 $x = round( $x, 3 ); // 313.138 $x = round( $x, - 2 ); // 300
21
Open Source Server Side Scripting 21 ECA 236 Numbers cont … PHP includes dozens of functions to use with numbers. EG: number_format( ) – groups number into thousands, comma takes one or two arguments number to be formatted number of decimal places $x = 43905.313; $x = number_format( $x ); // 43,905 $x = number_format( $x, 2 ); // 43,905.31
22
Open Source Server Side Scripting 22 ECA 236 Variables and CONSTANTS CONSTANTS Unlike variables, retain initial value throughout script Use define( ) to create Constants are usually named with all CAPS No $ prefixes a CONSTANT Cannot use echo( ) as you would with variables define( ‘NAME’, ‘Michael’ ); echo “Hello, “, NAME ; echo “Hello, “. NAME ;
23
Open Source Server Side Scripting 23 ECA 236 PHP’s Predefined Variables Superglobals $_SERVER $_GET $_POST $_COOKIE $_FILES $_ENV $_REQUEST $_SESSION
24
Open Source Server Side Scripting 24 ECA 236 Variable Scope Superglobal variables are visible everywhere Global variables within a script are visible throughout the script, but not inside functions Variable used inside functions are local to the function Variables used inside a function that are declared as global, refer to the global variables of the same name
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.