Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Language Basics.

Similar presentations


Presentation on theme: "PHP Language Basics."— Presentation transcript:

1 PHP Language Basics

2 PHP code starts with <?php
and ends with ?> written to a .php file <?php // statements ?>

3 anywhere in an HTML content
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 ?>

4 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

5 You can specify multiple items to print by separating them with commas
echo “First”, “second”, “third”; Firstsecondthird

6 There is a shorter way to
echo a variable <span><?php echo $name; ?></span> // shorter <span><?=$name?></span>

7 To check the type and content of a variable, use the var_dump() function
var_dump($a); // int(10)

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

9 The names of user-defined classes and functions, as well as
built-in constructs and keywords are case-insensitive

10 Thus, these three lines are equivalent: echo(“hello, world”);

11 Null keywords are also case-insensitive $val = null; // same

12 Variables, on the other hand, are case-sensitive.
That is, $name, $NAME, and $NaME are three different variables

13 to separate statements
PHP uses semicolons to separate statements

14 echo “Hello, world”; myFunction(42, “O'Reilly”); $a = 1; $name = “Elphaba”; $b = $a / 25.0; if ($a == $b) { echo “Rhyme? And Reason?”; }

15 Whitespace doesn’t matter
in a PHP program

16 For example, this statement: could just as well be written
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);

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

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

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

20 C comments: /* create an HTML form requesting
that the user confirm the action */ echo confirmationForm();

21 Variable names always begin with a dollar sign ($)
and are case-sensitive

22 Here are some valid variable names: $bill $head_count $MaximumForce
$I_HEART_PHP $_underscore $_int

23 Here are some illegal variable names:
$not valid $| $3wa

24 another of a different type
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);

25 You can reference the value of a variable whose name
is stored in another variable $foo = “bar”; $$foo = “baz”; // $bar = “baz”

26 What variable contains 100?
$foo = ‘bar’; $$foo = ‘world’; $$$foo = 100;

27 constant cannot change. Only scalar values can be constants
Once set, the value of a constant cannot change. Only scalar values (Booleans, integers, floats, and strings) can be constants

28 Constants are set using the define() function:
define(‘PUBLISHER’, “O’Reilly”); echo PUBLISHER;

29 Update: PHP 7 allows us to define array constants
define(‘NAME’, [‘One’,‘Two’,‘Three’]);

30 You cannot give a variable, function, class, or constant
the same name as a keyword

31 __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

32 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

33 include var include_once while instanceof xor static switch throw trait try unset() use

34 In PHP, arithmetic operators are very similar to that in Java
$b = 3; var_dump($a + $b); // 13 var_dump($a - $b); // 7 var_dump($a * $b); // 30 var_dump($a / $b); // var_dump($a % $b); // 1 var_dump($a ** $b); // 1000 var_dump(-$a); // -10

35 Assignment operators are also very similar

36 But be careful! $a = ‘1’; $b = 2; $c = $a + b; $d = $a . $b;

37 There are also incrementing and decrementing operators
$b--;

38 Comparison operators are similar, too
var_dump(2 < 3); // true var_dump(3 < 3); // false var_dump(3 <= 3); // true var_dump(4 <= 3); // false var_dump(2 > 3); // false var_dump(3 >= 3); // true var_dump(3 > 3); // false

39 And so are the logical operators
var_dump(true && true); var_dump(true && false); var_dump(true || false); var_dump(false || false); var_dump(!false);

40 But there are the PHP 7’s new “spaceship” operator
var_dump(1 <=> 5); var_dump(1 <=> 1); var_dump(5 <=> 2);

41 PHP 7 also introduces the new null coalesce operator
$uname = isset($_GET['uname']) ? $_GET['uname'] : 'guest'; var_dump($uname); $uname = $_GET['uname'] ?? 'guest'; $_GET['uname'] = 'admin';

42 When comparing values, we need to be careful with type juggling
var_dump($a == $b); var_dump($a === $b); var_dump($a != $b); var_dump($a !== $b); var_dump($a == $c); var_dump($a <> $c);

43 Strings are delimited by either single or double quotes ‘big dog’
“fat hog”

44 Variables are interpolated within double quotes, while
within single quotes they are not

45 Interpolation is the process of replacing variable names in the
string with the values of those variables

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

47 The other way is to surround the variable being interpolated with curly braces
echo “You are the $nth person”; echo “You are the {$n}th person”; You are the 12th person

48 What’s the output? $var = ‘world’; $$var = 100;
echo ‘Hello $var<br>’; echo “Hello $var<br>”; echo ‘Hello $world<br>’; echo “Hello $world<br>”;

49 To test whether two strings are equal, use the == operator
if ($a == $b) { echo “a and b are equal” }

50 Use the is_string() function
to test whether a value is a string if (is_string($x)) { // $x is a string }

51 gets the length of a string
The strlen() function gets the length of a string $s = ‘FILKOM UB’; echo strlen($s); // 9

52 The strtolower() function converts all characters in
a string to lowercase $s = ‘FILKOM UB’; echo strtolower($s); // filkom ub

53 The strtoupper() function converts all characters in a string to uppercase
$s = ‘filkom ub’; echo strtoupper($s); // FILKOM UB

54 The trim() function removes all blank spaces surrounding a string
$s = ‘ FILKOM UB ’; echo trim($s); // FILKOM UB

55 To concatenate a string, use the dot operators
$s1 = ‘FILKOM ’; $s2 = ‘UB’; $s3 = $s1 . $s2; // FILKOM UB

56 Use the is_bool() function to test whether a value is a Boolean or not
if (is_bool($x)) { // $x is a Boolean }

57 Use unset() to remove a variable $name = “Fred”;
unset($name); // throws a PHP notice

58 has been set to something,
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

59 values all evaluate to false:
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

60 $val = “”; if ($val) { // this won’t be executed }

61 bye.


Download ppt "PHP Language Basics."

Similar presentations


Ads by Google