Php intro overview
This presentation is the property of its rightful owner.
Sponsored Links
1 / 26

PHP Intro/Overview PowerPoint PPT Presentation


  • 63 Views
  • Uploaded on
  • Presentation posted in: General

PHP Intro/Overview. Bird Book pages 1-11, 39-106. Server-side Scripting. Everything you need to know in one slide Web server (with PHP “plug-in”) gets a URL http://www.site.com/page.php Web server see’s that the page is . php

Download Presentation

PHP Intro/Overview

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Php intro overview

PHP Intro/Overview

Bird Book pages 1-11, 39-106


Server side scripting

Server-side Scripting

Everything you need to know in one slide

  • Web server (with PHP “plug-in”) gets a URLhttp://www.site.com/page.php

  • Web server see’s that the page is .php

  • PHP Pre-processor searches the page for PHP code and executes the code.

  • Web server sends back the output of the PHP code, not the code itself.


Php basics

PHP Basics

  • PHP code can go anywhere in an HTML document as long as its in a PHP tag

  • Example

    <h1><?php echo “Hello World”; ?></h1>


Php variables

PHP Variables

  • Variables do NOT have strict typing

  • Unless math operations are used, variables are strings by default

  • Variables must start with $

    <?php

    $x = “Hello World”;

    echo $x;

    ?>


Commenting

Commenting

  • 3 Types

    // Comment

    # Comment

    /* Comment

    Comment */


Print and echo

Print and Echo

  • Print can print strings

    print “42”;

  • Echo is more robust than print, it can print numbers and more than one parameter, separated by commas

    echo 42; // This will actually print 42

    $x = “is”

    echo “The answer ”, $x, “ ”, 42;


Single quotes vs double

Single Quotes vs. Double

  • These are the same…

    print “This works.”;

    print ‘This works also.’;

  • This makes it easy to print quotes

    print “Here is a ‘single quote’ ”;

    print ‘Here is a “double quote” ’;


New lines

New lines

  • The string variable $var contains a new line character  “Hello there\n sir!”

    $var = “Hello there

    sir!”;

  • Introducing new lines breaks makes it easer to write SQL queries that work…


Sql example

SQL Example

$query = “ SELECT max(orderid)FROM orders

WHERE custid = $ID”;

  • If you send this query string to some database server it is important that the new line characters are in the string.


Variables in strings

Variables in strings

$x = 256

// “My computer has 256 MB ram.”

$message = “My computer has $x MB ram.”;

// “My computer has.”

$message = “My computer has $xMB ram.”;

Why?


Variables in strings1

Variables in strings

$x = 256

// “My computer has.”

$message = “My computer has $xMB ram.”;

// “My computer has 256MB ram.”

$message = “My computer has {$x}MB ram.”;


Variables in strings2

Variables in strings

Using { } is very important when trying to include complex variables into other strings.

$message = “Mars has a diameter of {$planets[‘Mars’][‘dia’]}.”;


Variables

Integers

$var1 = 10;

Floats

$var2 = 6.1;

Boolean

$var3 = true;

String

$var4 = “true”;

$var5 = ‘true’;

$var6 = ‘10’;

$var7 = “6.1”;

Variables


Constants

Constants

define(“PI”, 3.14159);

print PI;

// outputs 3.14159

Notice that constants don’t have $

Convention: Use all CAPS for constants

BTW, PHP is case sensitive


Expression and operators

Expression and Operators

  • Same as most high-level languages

    $z = 3;

    $x = 1;

    $y = 5.3;

    $z = $x + $y;

    $z++;

    $x += $y + 10;

    $z = $x * 10;


String concatenation

String concatenation

  • Not the same as JavaScript!

    $var1 = “Hello”;

    $var2 = “ world”;

    $var3 = $var1 + $var2; //This won’t work

    $var3 = $var1 . $var2;


Conditionals if statements

Conditionals (if statements)

  • Same as other high-level languages

    if ($var < 5) {

    print “The variable is less than 5”;

    }


Compound conditionals

Compound conditionals

if ($x == 1) {

print “x is 1”;

}

elseif ($x == 2) { //  Notice elseif is one word

print “x is 2”;

}

else {

print “x is not 1 and not 2”;

}


Loops

While

$c = 0;

while ($c < 10) {

print $c . “ ”;

$c++;

}

For

for ($c = 0; $c < 10; $c++) {

print $c . “ ”;

}

Loops


Loops break

Loops: break

for ($c = 0; $c < 10; $c++) {

print $c . “ ”;

if ($c == 5)

break;

}


Type conversion

Type Conversion

$year = 2003; // This is an integer

$yearString = strval($year);

// $yearString is “2003” not an integer


Type conversion1

Type Conversion

  • string strval(any other datatype)

  • integer intval(any other datatype)

  • float floatval(any other datatype)


Implicit type conversion

Implicit type conversion

  • $var = “100” + 15;

  • $var = 100 + 15.0;

  • $var = 39 . “ Steps”;

  • $var = 39 + “ Steps”;


Implicit type conversion1

Implicit type conversion

  • $var = “100” + 15; // $var becomes int

  • $var = 100 + 15.0; // $var becomes float

  • $var = 39 . “ Steps”; // $var becomes string

  • $var = 39 + “ Steps”; // $var become int


Functions

Functions

// Function definition

function fname ($parameter1, $parameter2) {

code;

code;

return $returnvalue;

}

// Function call

$x = fname(1,2);


Variable scope visibility

Variable Scope (visibility)

  • Same as Java

  • Same as C++

    function fun($x) {

    $temp = $x + 1;

    }

    fun(5);

    print “temp is: $temp”;


  • Login