PHP code starts with and ends with written to a .php file › ...PW-PHP-Basics.pdf · To put a...

Post on 30-Jun-2020

10 views 0 download

transcript

PHP code starts with <?php

and ends with ?>

written to a .php file

<?php// statements

?>

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 ?>

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

You can specify multiple items

to print by separating them

with commas

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

There is a shorter way to

echo a variable

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

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

To check the type and content of

a variable, use the var_dump()

function

$a = 10;

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

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;

The names of user-defined

classes and functions, as well as

built-in constructs and keywords

are case-insensitive

Thus, these three lines

are equivalent:

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

Null keywords are also

case-insensitive

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

Variables, on the other hand,

are case-sensitive.

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

are three different variables

PHP uses semicolons

to separate statements

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

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

}

Whitespace doesn’t matter

in a PHP program

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);

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)

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

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

C comments:/* create an HTML form requesting

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

Variable names always begin

with a dollar sign ($)

and are case-sensitive

Here are some

valid variable names:

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

Here are some illegal

variable names:

$not valid$|$3wa

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);

You can reference the value

of a variable whose name

is stored in another variable

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

What variable contains 100?

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

Once set, the value of a

constant cannot change.

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

can be constants

Constants are set using

the define() function:

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

Update: PHP 7 allows us to

define array constants

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

You cannot give a variable,

function, class, or constant

the same name as a keyword

__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

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

include var

include_once while

instanceof xor

static

switch

throw

trait

try

unset()

use

$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

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

Assignment operators are

also very similar

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

But be careful!

$a++;$b--;

There are also incrementing

and decrementing operators

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

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

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

But there are the PHP 7’s new

“spaceship” operator

$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

$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

Strings are delimited

by either single

or double quotes

‘big dog’“fat hog”

Variables are interpolated

within double quotes, while

within single quotes they are not

Interpolation is the process of

replacing variable names in the

string with the values

of those variables

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

Hi, GuidoHi, $name

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

What’s the output?

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

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

Strings are like arrays, i.e. each

character has an index

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

To test whether

two strings are equal,

use the == operator

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

}

Use the is_string() function

to test whether

a value is a string

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

}

The strlen() function

gets the length of a string

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

The strtolower() function

converts all characters in

a string to lowercase

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

The strtoupper() function

converts all characters in a

string to uppercase

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

The trim() function

removes all blank spaces

surrounding a string

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

To concatenate a string,

use the dot operators

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

Use the is_bool() function to

test whether a value is a

Boolean or not

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

}

Use unset() to

remove a variable

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

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

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

$val = “”;

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

}