![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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/1.jpg)
![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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/2.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/3.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/4.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/5.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/6.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/7.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/8.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/9.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/10.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/11.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/12.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/13.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/14.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/15.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/16.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/17.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/18.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/19.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/20.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/21.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/22.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/23.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/24.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/25.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/26.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/27.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/28.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/29.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/30.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/31.jpg)
__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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/32.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/33.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/34.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/35.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/36.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/37.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/38.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/39.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/40.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/41.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/42.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/43.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/44.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/45.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/46.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/47.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/48.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/49.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/50.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/51.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/52.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/53.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/54.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/55.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/56.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/57.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/58.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/59.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/60.jpg)
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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/61.jpg)
$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)](https://reader034.vdocuments.site/reader034/viewer/2022052613/5f16263e7282bb0c7c2d3955/html5/thumbnails/62.jpg)