Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PHP and MySQL (Creating Database-Driven Websites)

Similar presentations


Presentation on theme: "Introduction to PHP and MySQL (Creating Database-Driven Websites)"— Presentation transcript:

1 Introduction to PHP and MySQL (Creating Database-Driven Websites)

2  Write and execute a simple PHP script  Create statements and comments, and name variables  Use variables to store values  Choose between PHP’s data types  Understand the special NULL data type  Read GET and POST form input and store it in variables  Perform calculations and comparisons using operators  Use and override operator precedence rules

3 <?php PHP code... ?> You can also use the short version which looks like this: <? PHP code ?>

4 Q: This creature can change colour to blend in with its surroundings. What is its name? <?php //Print output echo ' A: Chameleon '; ?> Compile or Interpret??

5

6

7  A PHP script consists of one or more statements with each statement ending in a semicolon. Everything outside the tags is also ignored by the parser, and returned ‘as is’. Only the code between the tags is read and executed.  You can omit the semicolon on the last line of a PHP block since the closing ?> includes an implicit semicolon.  is perfectly valid PHP code.

8 <?php //This is a single-line comment #So is this /*And this is a multiline comment */ ?>

9 Variables are:  the building blocks of any programming language.  used to store both numeric and nonnumeric data.  contents of a variable can be altered during program execution  variables can be compared and manipulated using operators.

10  PHP supports a number of different variable types - Booleans, integers, floating point numbers, strings, arrays, objects, resources, and NULLs  PHP can automatically determine variable type by the context in which it is being used.

11  Every variable has a name which is preceded by a dollar ($) symbol  A variable name must begin with a letter or underscore character, optionally followed by more letters, numbers and underscores.  VALID $firstname, $one_day, $INCOME  INVALID $123 and $48hrs

12 Q: This creature has tusks made of ivory. What is its name? <?php //Define variable $answer = 'Elephant'; //Print output echo " $answer "; ?> Can use print() function instead

13

14 <?php $age = $dob + 15; ?> assignment operator variable Value - need not be fixed. Could be another variable, an expression or an expression involving other variables

15 <?php $today = "Feb 28 2010"; echo "Today is $today"; ?>

16 Enter your message: Most critical line !

17

18 <?php //Retrieve form data in a variable $input = $_POST['msg']; //Print it echo "You said: $input "; ?> $_POST and $_GET are special PHP variables - special types of predefined variables called global arrays.

19 Data TypeDescriptionExample BooleanThe simplest variable type in PHP, a Boolean variable simply specifies a true or false value. $auth = true; IntegerAn integer is simply a whole number like 75, -95, 2000 or 1. $age = 99; Floating-pointA floating-point number is typically a fractional number such as 12.5 or 3.149391239129. Floating point numbers may be specified using either decimal or scientific notation. $temperature = 56.89; StringA string is a sequence of characters, like 'hello' or 'abracadabra'. String values may be enclosed in either double quotes ("") or single quotes (''). $name = 'Harry';

20 <?php $auth = true; $age = 27; $name = 'Bobby'; $temp = 98.6; echo gettype($name). " "; echo gettype($auth). " "; echo gettype($age). " "; echo gettype($temp). " "; ?>

21  To explicitly mark a variable as numeric or string, use the settype() function.  PHP also supports a number of specialized functions to check if a variable or value belongs to a specific type: Function What It Does is_bool() Checks if a variable or value is Boolean is_string() Checks if a variable or value is a string is_numeric() Checks if a variable or value is a numeric string is_float() Checks if a variable or value is a floating point number is_int() Checks if a variable or value is an integer is_null() Checks if a variable or value is NULL is_array() Checks if a variable is an array is_object() Checks if a variable is an object

22  String values enclosed in double quotes are automatically parsed for variable names. If variable names are found they are automatically replaced with the appropriate variable value.  cf. to what happens when string values are enclosed in single quotes………

23 <?php $identity = 'James Bond'; $car = 'BMW'; //This would contain the string "James Bond drives a BMW" $sentence = "$identity drives a $car"; echo $sentence. “ ”; //This would contain the string "$identity drives a $car" $sentence = '$identity drives a $car'; echo $sentence; ?>

24

25 <?php //Will cause an error due to mismatched quotes $statement = 'It's hot outside'; echo $statement; //Will be fine $statement = 'It\'s hot outside'; echo $statement; ?> If your string contains quotes, carriage returns or backslashes, it is necessary to escape these special characters with a backslash.

26

27 <?php //Will cause an error due to mismatched quotes //$statement = 'It's hot outside'; //echo $statement; //Will be fine $statement = 'It\'s hot outside'; echo $statement; ?>

28

29  The NULL data type means that a variable has no value.  A NULL is typically seen when a variable is initialized but not yet assigned a value, or when a variable has been de-initialized with the unset() function.

30 <?php //Check type of uninitialized variable - returns NULL echo gettype($me). " "; //Assign a value $me = 'David'; //Check type again - returns STRING echo gettype($me). " "; //Deinitialize variable unset($me); //Check type again - returns NULL echo gettype($me). " "; ?> Using unset(); Using gettype();

31

32  PHP comes with over 15 operators - including operators for assignment, arithmetic, string, comparison and logical operations.

33 OperatorWhat It Does =Assignment +Addition -Subtraction *Multiplication /Division; returns quotient %Division; returns modulus.String concatenation

34 OperatorWhat It Does ==Equal to ===Equal to and of the same type !==Not equal to or not of the same type <> aka !=Not equal to <Less than <=Less than or equal to >Greater than

35 OperatorWhat It Does >=Greater than or equal to &&Logical AND ||Logical OR xorLogical XOR !Logical NOT ++Addition by 1 --Subtraction by 1

36 <?php $num1 = 101; $num2 = 5; $sum = $num1 + $num2; echo $sum." "; $diff = $num1 - $num2; echo $diff." "; $product = $num1 * $num2; echo $product." "; $quotient = $num1 / $num2; echo $quotient." "; $remainder = $num1 % $num2; echo $remainder." "; ?>

37

38 The following two code snippets are equivalent: <?php $a = $a + 10; ?> <?php $a += 10; ?> Addition and assignment simultaneously

39 <?php $username = 'jsmith'; $domain = 'bedford.ac.uk'; //Combine them using the concatenation operator $email = $username. '@'. $domain; echo $email; ?> Concatenation

40

41 <?php //Define string $str = 'the'; //Add and assign $str.= 'n'; //$str now contains "then" echo $str; ?> Concatenation and assignment simultaneously

42

43 <?php //Define some variables $mean = 29; $median = 40; $mode = 29; //Less-than operator returns true if left side is less than right //Returns true here $result = ($mean < $median); echo "Answer: ".$result." ";

44  IMPORTANT - The result of a comparison test is always a Boolean value (either true or false).  This makes comparison operators a critical part of your toolkit since you can use them in combination with a conditional statement to send a script down any one of many action paths.

45 The === Operator  An important comparison operator in PHP 4.0 is the === operator which enables you to test both for equality and type.

46 <?php $user = 'joe'; $pass = 'trym3'; $saveCookie = 1; $status = 1; //Logical AND returns true if all conditions are true //Returns true here $result = (($user == 'joe') && ($pass == 'trym3')); echo "Answer: ".$result." ";

47 //Logical OR returns true if any condition is true //Returns false here $result = (($status < 1) || ($saveCookie == 0)); echo "Answer: ".$result." "; //Logical NOT returns true if the condition is false and vice-versa //Returns false here $result = !($saveCookie == 1); echo "Answer: ".$result." ";

48 //Logical XOR returns true if any of the two conditions are true //Returns false if both conditions are true //Returns false here $result = (($status == 1) XOR ($saveCookie == 1)); echo "Answer: ".$result." "; ?>

49 <?php //Define $total as 10 $total = 10; //Increment it $total++; //$total is now 11 echo "Total now is: ".$total; ?>

50 Note: is functionally equivalent to. These operators are frequently used in loops to update the value of the loop counter.

51 You can override these rules with parentheses. This reduces ambiguity and ensures that operators are evaluated in the order that you specify. For example, the expression 10 * 10 + 1 would return 101 while the expression 10 * (10 + 1) would return 110. '!' '++' '--' '*' '/' '%' '+' '−' '.' ' ' '>=' '==' '!=' '===' '!==' '&&' '||' '?' ':'


Download ppt "Introduction to PHP and MySQL (Creating Database-Driven Websites)"

Similar presentations


Ads by Google