php code starts with and ends with written to a .php file › ...pw-php-basics.pdf · to put a...

62

Upload: others

Post on 30-Jun-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)
Page 2: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

PHP code starts with <?php

and ends with ?>

written to a .php file

<?php// statements

?>

Page 3: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

PHP code can be placed

anywhere in an HTML content

<?php // PHP can be placed here ?><html><head></head><body>

<?php // or here... ?></body></html><?php // or even here ?>

Page 4: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

To put a string into the HTML

of a PHP-generated page,

use echo(It’s like Java’s System.out.println)

echo “Printy”;echo(“Printy”); // also valid

Page 5: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

You can specify multiple items

to print by separating them

with commas

echo “First”, “second”, “third”;Firstsecondthird

Page 6: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

There is a shorter way to

echo a variable

<span><?php echo $name; ?></span>

// shorter<span><?=$name?></span>

Page 7: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

To check the type and content of

a variable, use the var_dump()

function

$a = 10;

var_dump($a);// int(10)

Page 8: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

We can easily put multiline strings

into your program with a heredoc(here documents)

$html = <<< EndOfQuote<html><head>

</head><body>Hello World!

</body></html>EndOfQuote;

Page 9: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

The names of user-defined

classes and functions, as well as

built-in constructs and keywords

are case-insensitive

Page 10: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Thus, these three lines

are equivalent:

echo(“hello, world”);ECHO(“hello, world”);EcHo(“hello, world”);

Page 11: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Null keywords are also

case-insensitive

$val = null; // same$val = Null; // same$val = NULL; // same

Page 12: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Variables, on the other hand,

are case-sensitive.

That is, $name, $NAME, and $NaME

are three different variables

Page 13: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

PHP uses semicolons

to separate statements

Page 14: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

echo “Hello, world”;myFunction(42, “O'Reilly”);$a = 1;$name = “Elphaba”;$b = $a / 25.0;

if ($a == $b) {echo “Rhyme? And Reason?”;

}

Page 15: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Whitespace doesn’t matter

in a PHP program

Page 16: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

For example, this statement:raisePrices($inventory, $inflation, $costOfLiving, $greed);

could just as well be written

with more whitespace:raisePrices (

$inventory ,$inflation ,$costOfLiving ,$greed

) ;

or with less whitespace:raisePrices($inventory,$inflation,$costOfLiving,$greed);

Page 17: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

PHP provides several ways

to include comments

within your code(all of which are borrowed from existing languages

such as C, C++, and the Unix shell)

Page 18: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Shell-style comments:# create an HTML form requesting# that the user confirm the actionecho confirmationForm();

Page 19: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

C++ comments:// create an HTML form requesting// that the user confirm the actionecho confirmationForm();

Page 20: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

C comments:/* create an HTML form requesting

that the user confirm the action */echo confirmationForm();

Page 21: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Variable names always begin

with a dollar sign ($)

and are case-sensitive

Page 22: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Here are some

valid variable names:

$bill$head_count$MaximumForce$I_HEART_PHP$_underscore$_int

Page 23: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Here are some illegal

variable names:

$not valid$|$3wa

Page 24: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

You can replace a

variable’s value with

another of a different type(known as type juggling)

$what = “Fred”;$what = 35;$what = array(“Fred”, 35);

Page 25: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

You can reference the value

of a variable whose name

is stored in another variable

$foo = “bar”;$$foo = “baz”; // $bar = “baz”

Page 26: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

What variable contains 100?

$foo = ‘bar’;$$foo = ‘world’;$$$foo = 100;

Page 27: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Once set, the value of a

constant cannot change.

Only scalar values(Booleans, integers, floats, and strings)

can be constants

Page 28: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Constants are set using

the define() function:

define(‘PUBLISHER’, “O’Reilly”);echo PUBLISHER;

Page 29: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Update: PHP 7 allows us to

define array constants

define(‘NAME’, [‘One’, ‘Two’, ‘Three’]);

Page 30: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

You cannot give a variable,

function, class, or constant

the same name as a keyword

Page 31: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

__CLASS__ and endforeach

__DIR__ array() endif

__FILE__ as endswitch

__FUNCTION__ break endwhile

__LINE__ echo eval()

__METHOD__ else exit()

__NAMESPACE__ elseif extends

__TRAIT__ empty() final

__halt_compiler() enddeclare insteadof

abstract endfor interface

Page 32: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

isset() require_once default

list() return die()

namespace callable do

new case for

or catch foreach

print class function

private clone global

protected const goto

public continue if

require declare implements

Page 33: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

include var

include_once while

instanceof xor

static

switch

throw

trait

try

unset()

use

Page 34: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$a = 10;$b = 3;var_dump($a + $b); // 13var_dump($a - $b); // 7var_dump($a * $b); // 30var_dump($a / $b); // 3.333333...var_dump($a % $b); // 1var_dump($a ** $b); // 1000 (since PHP 5.6)var_dump(-$a); // -10

In PHP, arithmetic operators are

very similar to those in Java

Page 35: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$a = 3 + 4 + 5 - 2;$a = 13;$a += 14;$a -= 2;$a *= 4;

Assignment operators are

also very similar

Page 36: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$a = ‘1’;$b = 2;$c = $a + b;$d = $a . $b;

But be careful!

Page 37: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$a++;$b--;

There are also incrementing

and decrementing operators

Page 38: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

var_dump(2 < 3); // truevar_dump(3 < 3); // falsevar_dump(3 <= 3); // truevar_dump(4 <= 3); // falsevar_dump(2 > 3); // falsevar_dump(3 >= 3); // truevar_dump(3 > 3); // false

Comparison operators are

similar, too

Page 39: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

var_dump(true && true);var_dump(true && false);var_dump(true || false);var_dump(false || false);var_dump(!false);

And so are the logical

operators

Page 40: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

var_dump(1 <=> 5);var_dump(1 <=> 1);var_dump(5 <=> 2);

But there are the PHP 7’s new

“spaceship” operator

Page 41: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$uname = isset($_GET[‘uname’]) ? $_GET[‘uname’] : ‘guest’;var_dump($uname);

$uname = $_GET[‘uname’] ?? ‘guest’;var_dump($uname);

$_GET[‘uname’] = ‘admin’;$uname = $_GET[‘uname’] ?? ’guest’;var_dump($uname);

PHP 7 also introduces the

new null coalescing operator

Page 42: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$a = 3;$b = ‘3’;$c = 5;var_dump($a == $b);var_dump($a === $b);var_dump($a != $b);var_dump($a !== $b);var_dump($a == $c);var_dump($a <> $c);

When comparing values, we need

to be careful with type juggling

Page 43: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Strings are delimited

by either single

or double quotes

‘big dog’“fat hog”

Page 44: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Variables are interpolated

within double quotes, while

within single quotes they are not

Page 45: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Interpolation is the process of

replacing variable names in the

string with the values

of those variables

Page 46: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$name = ‘Guido’;echo “Hi, $name”;echo ‘Hi, $name’;

Hi, GuidoHi, $name

Page 47: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

The other way is to surround

the variable being interpolated

with curly braces

$n = 12;echo “You are the $nth person”;echo “You are the {$n}th person”;

You are the 12th person

Page 48: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

What’s the output?

$var = ‘world’;$$var = 100;

echo ‘Hello $var<br>’;echo “Hello $var<br>”;echo ‘Hello $world<br>’;echo “Hello $world<br>”;

Page 49: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Strings are like arrays, i.e. each

character has an index

$s = ‘Hello’;echo $s[0]; // Hecho $s[-1]; // o

Page 50: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

To test whether

two strings are equal,

use the == operator

if ($a == $b) {echo “a and b are equal”

}

Page 51: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Use the is_string() function

to test whether

a value is a string

if (is_string($x)) {// $x is a string

}

Page 52: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

The strlen() function

gets the length of a string

$s = ‘FILKOM UB’;echo strlen($s); // 9

Page 53: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

The strtolower() function

converts all characters in

a string to lowercase

$s = ‘FILKOM UB’;echo strtolower($s);// filkom ub

Page 54: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

The strtoupper() function

converts all characters in a

string to uppercase

$s = ‘filkom ub’;echo strtoupper($s);// FILKOM UB

Page 55: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

The trim() function

removes all blank spaces

surrounding a string

$s = ‘ FILKOM UB ’;echo trim($s);// FILKOM UB

Page 56: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

To concatenate a string,

use the dot operators

$s1 = ‘FILKOM ’;$s2 = ‘UB’;$s3 = $s1 . $s2;// FILKOM UB

Page 57: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Use the is_bool() function to

test whether a value is a

Boolean or not

if (is_bool($x)) {// $x is a Boolean

}

Page 58: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

Use unset() to

remove a variable

$name = “Fred”;unset($name);echo $name; // throws a PHP notice

Page 59: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

To see if a variable

has been set to something,

use isset()

$s1 = isset($name); // $s1 is false$name = “Fred”;$s2 = isset($name); // $s2 is true

Page 60: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

In PHP, the following

values all evaluate to false:• The keyword false

• The integer 0

• The floating-point value 0.0

• The empty string (“”) and the string “0”

• An array with zero elements

• An object with no values or functions

• The NULL value

Page 61: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)

$val = “”;

if ($val) {// this won’t be executed

}

Page 62: PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a string into the HTML of a PHP-generated page, use echo (It’s like Java’s System.out.println)