0% found this document useful (0 votes)
3 views8 pages

Computer Programming

The document provides an overview of PHP, a server-side scripting language used for web development, detailing its syntax, features, and capabilities. It covers basic concepts such as variable scope, conditional statements, loops, and methods for sending information to a web server. Additionally, it highlights the importance of setting up a web server environment for running PHP scripts.

Uploaded by

Kish fish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Computer Programming

The document provides an overview of PHP, a server-side scripting language used for web development, detailing its syntax, features, and capabilities. It covers basic concepts such as variable scope, conditional statements, loops, and methods for sending information to a web server. Additionally, it highlights the importance of setting up a web server environment for running PHP scripts.

Uploaded by

Kish fish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COMPUTER PROGRAMMING - 4TH QUARTER

HYPERTEXT PREPROCESSOR (PHP)


PHP Overview
- Definition: PHP is a widely used server-side
scripting language designed primarily for
web development.
- Execution: PHP scripts are executed on the
server, and the output is sent to the browser
as plain HTML.
- Cost: PHP is available as a free download.
- File Contents: A PHP file can include text,
HTML, CSS, JavaScript, and PHP code.
- File Extension: PHP files use the .php
extension.
BASIC PHP SYNTAX
PHP IS POPULAR
- A PHP script can be placed anywhere
- Easy to learn for beginners
- Works well with HTML in the document.
- Supported by most hosting services - A PHP script starts with <?php and
- Strong community support ends with ?>.
Ex:
KEY FEATURES <!DOCTYPE html>
- Open-source <html>
- Cross-platform <body>
- Supports multiple databases
- Fast and efficient for web tasks <h1>My first PHP page</h1>
WHAT DOES PHP DO?
<?php
PHP runs on a web server and is used to:
- Create dynamic web pages echo "Hello World!";
- Handle forms and user input ?>
- Connect to databases (e.g., MySQL)
- Manage sessions and user authentication </body>
- Build full web applications </html>

Set Up Your Environment: Output:


To run PHP scripts, you need a web server with My first PHP page
PHP installed. Popular options include:
1. XAMPP: A free and open-source cross- Hello World!
platform web server package that includes
Apache, MySQL, and PHP.
2. MAMP: A similar package for macOS and COMMENTS IN PHP
Windows. - A line that is not read/executed as part of the
3. LAMP/LEMP Stack: For Linux users, these program. Its purpose is to be read by
stacks include Apache/Nginx, someone who is looking at the code.
MySQL/MariaDB, and PHP. Ex:
Once installed: - // This is a single-line comment
- Place your PHP files in the server's root directory - # This is also a single-line comment
(e.g., htdocs for XAMPP or MAMP). - /* This is a multiple-line comment block
- Start the web server and access your scripts via that spans over multiple lines */
[Link] - // You can also use comments to leave out
parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
TWO WAYS TO DISPLAY OUTPUT IN PHP
• echo Statement – has no return
value. It can take multiple
parameters and is marginally faster
than print.
• print Statement – has a return value
of 1, so it can be used in
expressions, and it can only take 1
parameter or argument.

PHP CASE SENSITIVITY


- Keywords, Functions, and Classes -
PHP, keywords (e.g., if, else, while, echo), cla
sses, functions, and user-defined
functions are NOT case-sensitive.
This means the following three echo statements
are all legal and produce the same output:
What is Variable Scope?
<!DOCTYPE html> Hello World! - In PHP, variables can be declared
<html> anywhere in the script.
<body> Hello World!
Hello World! - Scope = the part of the script where a
<?php variable can be referenced/used.
ECHO "Hello World!<br>"; - PHP has three different variable
echo "Hello World!<br>";
EcHo "Hello World!<br>"; scopes:
?> - Local
- Global
</body>
</html> - Static

Variables
However, all variable names in PHP are case-
sensitive.
In the example below, $color, $COLOR,
and $coLOR are treated as three completely
different variables. Only the first echo statement
will display the value of the $color variable.

<!DOCTYPE html> My car is red


<html> My house is
<body>
My boat is
<?php
$color = "red"; Note: The second
echo " and third lines are
. "<br>"; empty
echo "My boat is " .
$coLOR . "<br>";
because $COLOR an
?> d $coLOR have not
been assigned any
</body> values.
</html>
CONDITIONAL STATEMENTS
if
Description: Executes code only if a single
condition is true.
Example:
<?php $age = 18; if ($age >= 18) { echo "You
can vote."; } ?

if...else
Description: Executes one block of code if
the condition is true, and another block if the
condition is false.
Example:
<?php $age = 16; if ($age >= 18) { echo "You
can vote."; } else { echo "You cannot
vote."; } ?>

if...elseif...else
Description: Tests multiple conditions and
executes different code blocks for each
condition.
Example:
<?php $score = 85; if ($score >= 90) { echo
"Grade A"; } elseif ($score >= 80) { echo
XAMPP (CROSS-PLATFORM, APACHE, "Grade B"; } elseif ($score >= 70) { echo
MariaDB, PHP, AND PERL) "Grade C"; } else { echo "Grade F"; } ?>
- A free and open source cross-platform
web server solution stack package LOOPS
developed by Apache friends. - Often, when you write code, you want
- It is a simple, lightweight Apache the same block of code to run over and
distribution that makes it extremely easy over again a certain number of times.
for developers to create a local web So, instead of adding several almost
server for testing and deployment equal code lines in a script, we can
purposes. use loops.
- Once XAMPP is active, you can access - while loop – executes a block of code
your local copy with a browser using a as long as the specified condition is
URL like true.
[Link] or [Link]
DO WHILE LOOP TWO WAYS THE BROWSER CLIENT CAN
- This loop will always execute the block SEND INFORMATION TO THE WEB
of code once, it will then check the SERVER
condition, and repeat the loop while the
specified condition is true. GET method & POST method (refer to ppt
for examples)
- Used to transfer data from the client to the
server in the HTTP protocol
URL encoding
Converting special characters in URLs into a
format that can be safely transmitted over the
internet.
FOR LOOP  URL encoding ensures that URLs are
 Loops through a block of code a valid and that data is correctly
specified number of times. interpreted by web servers and
 This loop is used when you know in browsers.
advance how many times the script  Special characters are replaced with a
should run. % symbol followed by their two-digit
 init counter: Initialize the loop counter hexadecimal ASCII value.
value  Example: a space ( ) is encoded as
 test counter: Evaluated for each loop %20
iteration. If it evaluates to TRUE, the  GET method – This method is used to
loop continues. If it evaluates to FALSE, retrieve data from a server. Information
the loop ends. sent from a form with the GET method
 increment counter: Increases the loop is visible to everyone (all variable
counter value names and values are displayed in the
URL). It has limits on the amount of
information to send. It may be used for
sending non-sensitive data.
 POST method - This method is used to
send data to a server to create or
update resources. Information sent
from a form with the POST method is
invisible to others (all names/values are
embedded within the body of the HTTP
request) and has no limits on the
amount of information to send

GET & POST GET METHOD


POST METHOD

DATABASE
• A collection of information that is organized so that it can be easily accessed, managed,
and updated.
CREATING A DATABASE & DBASE USERNAME

You might also like