0% found this document useful (0 votes)
35 views33 pages

Fundamentals of PHP

The document provides an overview of PHP fundamentals, including setup requirements, basic syntax, variable declaration, and scope. It explains the use of comments, the difference between echo and print statements, and the case sensitivity of variables. Additionally, it covers the global and local scope of variables and the use of static variables within functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views33 pages

Fundamentals of PHP

The document provides an overview of PHP fundamentals, including setup requirements, basic syntax, variable declaration, and scope. It explains the use of comments, the difference between echo and print statements, and the case sensitivity of variables. Additionally, it covers the global and local scope of variables and the use of static variables within functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Fundamentals of PHP

What do I need?
▫ To start using PHP, you can:
▫ Find a web host with PHP and
MySQL support
▫ Install a web server on your own PC,
and then install PHP and MySQL

2
Basic PHP Syntax
▫ A PHP script can be placed anywhere in
the document.
▫ A PHP script starts with <?php and
ends with ?>

3
Basic PHP Syntax
▫ The default file extension for PHP files
is ".php".
▫ A PHP file normally contains HTML
tags, and some PHP scripting code.

4
Example:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html> 5
PHP Case Sensitivity
▫ In PHP, NO keywords (e.g. if, else, while,
echo, etc.), classes, functions, and user-
defined functions are case-sensitive.

▫ Note: However; all variable names are


case-sensitive!
6
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html> 7
Comments in PHP
▫ A comment in PHP code is a line that is
not executed as a part of the program.
Its only purpose is to be read by
someone who is looking at the code.

8
Comments can be used to:
▫ Let others understand your code
▫ Remind yourself of what you did - Most
programmers have experienced coming back
to their own work a year or two later and
having to re-figure out what they did.
Comments can remind you of what you were
thinking when you wrote the code
9
Syntax for single-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
?>
</body>
</html>
10
Syntax for multiple-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
</body>
11
</html>
Using comments to leave out parts of
the code:
<!DOCTYPE html>
<html>
<body>
<?php
// You can also use comments to leave out parts of a code line

$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
12
PHP Variables
▫ Variables are "containers" for storing
information.

13
Creating (Declaring) PHP Variables
▫ In PHP, a variable starts with the $ sign,
followed by the name of the variable:

14
Example:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
15
PHP Variables
▫ A variable can have a short name (like x
and y) or a more descriptive name (age,
carname, total_volume).

16
Rules for PHP variables:
▫ A variable starts with the $ sign,
followed by the name of the variable
▫ A variable name must start with a letter
or the underscore character

17
Rules for PHP variables:
▫ A variable name cannot start with a number
▫ A variable name can only contain alpha-
numeric characters and underscores (A-z,
0-9, and _ )
▫ Variable names are case-sensitive ($age
and $AGE are two different variables)
18
PHP is a Loosely Typed Language
▫ PHP automatically associates a data
type to the variable, depending on its
value. Since the data types are not set
in a strict sense, you can do things like
adding a string to an integer without
causing an error.
19
PHP Variables Scope
▫ In PHP, variables can be declared
anywhere in the script.
▫ The scope of a variable is the part of
the script where the variable can be
referenced/used.

20
PHP has three different variable scopes:

1. local
2. global
3. static
21
Global and Local Scope
▫ A variable declared outside a function
has a GLOBAL SCOPE and can only be
accessed outside a function:

22
Example:
<?php
$x = 5; // global scope
function myTest() {
//using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}

myTest();
echo "<p>Variable x outside function is: $x</p>";
23
?>
Global and Local Scope
▫ A variable declared within a function
has a LOCAL SCOPE and can only be
accessed within that function:

24
Example:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an


error
echo "<p>Variable x outside function is: $x</p>";
?>
25
The global Keyword
▫ The global keyword is used to access a
global variable from within a function.
▫ To do this, use the global keyword
before the variables (inside the
function):

26
Example:
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
27
?>
The static Keyword
▫ Normally, when a function is
completed/executed, all of its variables
are deleted. However, sometimes we
want a local variable NOT to be deleted.
We need it for a further job.

28
The static Keyword
▫ To do this, use the static keyword when
you first declare the variable:

29
Example:
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?> 30
PHP echo and print Statements
▫ With PHP, there are two basic ways to
get output: echo and print.
▫ echo and print are more or less the
same. They are both used to output
data to the screen.

31
PHP echo and print Statements
▫ The differences are small: echo has no
return value while print has a return
value of 1 so it can be used in
expressions.

32
PHP echo and print Statements
▫ echo can take multiple parameters
(although such usage is rare) while print
can take one argument. echo is
marginally faster than print.

33

You might also like