Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views7 pages

ITELEC4 Mod 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Golden West Colleges

College of Information Technology Education


Alaminos City, Pangasinan

IT Major Elective 4
(Web Systems & Development
2)
Module 1

Prepared by:
Kairos Jil I. Molina
molinakairos@gmail.com
Lesso
n PHP

1
What is PHP?
PHP is a general-purpose scripting language especially suited to web development. It was
originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference
implementation is now produced by The PHP Group. PHP originally stood for “Personal Home
Page”.
 PHP code is usually processed on a web server by a PHP interpreter implemented as a module,
a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of
the interpreted and executed PHP code – which may be any type of data, such as
generated HTML or binary image data – would form the whole or part of an HTTP response.
 PHP is an acronym for "PHP: Hypertext Preprocessor"
 PHP is a widely-used, open-source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use

The PHP language evolved without a written formal specification or standard until 2014, with the original
implementation acting as the de facto standard which other implementations aimed to follow. Since 2014,
work has gone on to create a formal PHP specification.

As of January 2021, 72% of PHP websites use discontinued versions of PHP, i.e., PHP 7.2 or lower,
which are no longer supported by The PHP Development Team. A large additional fraction uses PHP 7.3,
which is only (up to December 6, 2021) "supported for critical security issues only." Over 40% of all PHP
websites use version 5.6 or older, that not even Debian supports (Debian 9 supported version 7.0 and
7.1).

PHP is an amazing and popular language!


It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)!
It is deep enough to run the largest social network (Facebook)!
It is also easy enough to be a beginner's first server-side language!

XAMPP is an abbreviation for cross-platform, Apache, MySQL, PHP and Perl, and it allows you to build
WordPress site offline, on a local web server on your computer.

What is PHP File?


 PHP files can contain text, HTML, CSS, JavaScript, and PHP code
 PHP code is executed on the server, and the result is returned to the browser as plain HTML
 PHP files have extension ".php"

What Can PHP Do?


 PHP can generate dynamic page content
 PHP can create, open, read, write, delete, and close files on the server
 PHP can collect form data
 PHP can send and receive cookies
 PHP can add, delete, modify data in your database
 PHP can be used to control user-access
 PHP can encrypt data
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies.
You can also output any text, such as XHTML and XML.

Why PHP?
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 PHP supports a wide range of databases
 PHP is easy to learn and runs efficiently on the server side

What’s new in PHP 7?


 PHP 7 is much faster than the previous popular stable release (PHP 5.6)
 PHP 7 has improved Error Handling
 PHP 7 supports stricter Type Declarations for function arguments
 PHP 7 supports new operators (like the spaceship operator: <=>)

How to run your .php files?


 Make sure that you already installed XAMPP Server on your pc.
 Run XAMPP server and start Apache and MySQL service.
 Place your .php file on C:\xampp\htdocs
 Open your web browser of choice and in the URL type the following:
127.0.0.1/file_name.php

Alternative:
 Download USBWebserver
 https://drive.google.com/file/d/1xuopwuEYtHOlj34yK0qE-dZ3atn36_kw/view?usp=sharing
 Place your .php files in the \root folder
 Press “Localhost” to view your web app in your browser
 Press “PHPMyAdmin” to manage your database

Lesso
n PHP Syntax
2
A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax


 A PHP script can be placed anywhere in the document.
 A PHP script starts with <?php and ends with ?>:
Sample code:

<?php
// PHP code goes here
?>

 The default file extension for PHP files is ".php".


 A PHP file normally contains HTML tags, and some PHP scripting code.
 Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!" on a web page.
Sample code:
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

Note: PHP statements end with a semicolon (;).

PHP Case Sensitivity


 In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions
are not case-sensitive.
 In the example below, all three echo statements below are equal and legal.
Sample code:

<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>

Note: However; all variable names are case-sensitive!

Look at the example below; only the first statement will display the value of the $color variable! This is
because $color, $COLOR, and $coLOR are treated as three different variables:

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

Lesso
n PHP Comments
and Variables
3
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.

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

PHP supports several ways of commenting:

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>

Syntax for multiple-line comments:


<!DOCTYPE html>
<html>
<body>

<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>

</body>
</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>

PHP Variables
 Variables are "containers" for storing information.

Creating (Declaring) PHP Variables


 In PHP, a variable starts with the $ sign, followed by the name of the variable.
Sample code:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>

After the execution of the statements above, the variable $txt will hold the value Hello world!, the
variable $x will hold the value 5, and the variable $y will hold the value 10.5.

Note: When you assign a text value to a variable, put quotes around the value.

Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created
the moment you first assign a value to it.

Think of variables as containers for storing data.

 A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
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
 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)

Output Variables:
 The PHP echo statement is often used to output data to the screen.
 The following example will show how to output text and a variable.
Sample code:

<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>

The following example will produce the same output as the example above:

<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>

The following example will output the sum of two variables:

<?php
$x = 5;
$y = 4;
echo $x + $y;
?>

PHP is a Loosely Typed Language


 In the example above, notice that we did not have to tell PHP which data type the variable is.
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.
 In PHP 7, type declarations were added. This gives an option to specify the data type expected
when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a
type mismatch.

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.
PHP has three different variable scopes:
 local
 global
 static

Global and Local Scope


 A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside
a function:
Example:
Variable with global scope

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


?>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that
function:

Example
Variable with local scope:

<?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>";
?>
You can have local variables with the same name in different functions, because local variables are only
recognized by the function in which they are declared.

PHP 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).
Sample code:
<?php
$x = 5;
$y = 10;

function myTest() {
global $x, $y;
$y = $x + $y;
}

myTest();
echo $y; // outputs 15
?>

You might also like