Presentation is loading. Please wait.

Presentation is loading. Please wait.

Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.

Similar presentations


Presentation on theme: "Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control."— Presentation transcript:

1 Just a Little PHP Programming PHP on the Server

2 Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control Statements Functions and Subroutines Macros and Preprocessor Commands I/O Statements Libraries External Tools (Compiler, debugger etc)

3 PHP like JavaScript is Embedded in Web Pages The difference is that a PHP file is executed by the Server BEFORE being sent to the client. Just like CGI. <?php //Your PHP goes here ?>

4 Comments // This is a single line comment /* This is a multiline comment just like C */

5 Data Types PHP is a weakly typed language. Any variable can be any data type The available data types are: boolean number (integer and float) string array object

6 Variable Names To declare a scalar variable just use it. Variable names begin with a $, ie: $x=7; echo $counter;

7 Constants Numeric: 4.2 17.5 1.6E+7 21 0x2A String: “Hello” ‘Nice Day’ Bob define(“MYCONSTANT”, 42); echo MYCONSTANT If a constant is not defined it stands for itself, ie: Bob. Otherwise it stands for its value. Constants should be ALLCAPS and do not use a $ before the name

8 Creating an Array $list[0] = 17; //This creates an array $list = array(1,2,3 4,5); //creates an indexed array $list = array(“Smith” => “Carpenter, “Jones”=> “Technician”, “Rajiv” => “Manager”); //Associative array $n=sizeof($list);

9 Operators Arithmetic: Regular C style operators = + - * / % += -= *= /= ++ -- Relational: == != <> > = <= Note: “4” == 4 is True String: “a”. $b $a.=$b; Logical: && || !

10 Web Page Output echo “The size of my array is: “. $n; print “Hello World: “. $n; printf “There are %4d items in my array, %s\n”, $n, $name; printf uses the same format codes that C uses: %d (integer) %f (float) %x (hex) %s (string) (There is one new format code: %b - binary)

11 if/then/else if ($price 20) { printf “%10.2f is a reasonable price ”, $price; $total+=$price; } else echo “Try again!”;

12 Simple for loops for($i=1;$i<10;$i++) { $sum+=$i; echo “Running total is: “ + $sum + “ ”; } echo “Final total is: “ $sum;

13 Looping Through Associative Arrays foreach( $array as $key => $value) { echo “Key: “. $key echo “Value: “. $value; } Note: Associative Arrays and Indexed Arrays are the same thing. The key values for an indexed array are: 0, 1, 2.....

14 Functions in PHP function myFunction($arg1, $arg2, $arg3) { //Do some calculation ie: $result= ($arg1 + $arg2) % $arg3; return $result; }

15 Including a library of functions include (“myLibraryFile.php”); require(“myLibrary.php”); require_once(“myLibrary.php”); Use any of:

16 Important PHP VariablesPurpose $_GET, $_POST, $_COOKIE $_REQUEST Data sent between client and server. $_REQUEST includes all 3 $REMOTE_ADDRIP of the client $HTTP_REFERERPrevious Web Page $REQUEST_TIMEWhen page was requested (EPOCH time) $HTTP_USER_AGENTBrowser Info $SCRIPT_FILENAMECurrent PHP script file

17 SQL Functions MYSQL FUNCTION CALLEXPLANATION mysql_error()Report on last error mysql_connect(host,user, pass)connects to database; returns a connection to the database mysql_selectdb(dbName)use specified schema $result=mysql_query(stmt)runs stmt against the database returns 0 to n rows for SELECT returns number of rows affected for INSERT, UPDATE or DELETE $row=mysql_fetch_array($resu lt) creates an associative array using column names as keys mysql_close($con)closes the database connection

18 SQL Functions Some PHP FunctionsEXPLANATION date(‘r’)The current date as a string time()current time die(message)print out a message and quit include(‘fileName’) require_once(‘fileName’); include an external PHP file – allows shared code and libraries

19 THE END


Download ppt "Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control."

Similar presentations


Ads by Google