0% found this document useful (0 votes)
5 views

Introduction to PHP Basics

This document is a guide to the basics of PHP, covering its role in web development, syntax, variables, and basic functions. It outlines the materials needed for learning, the installation of local web servers like XAMPP and WAMP, and the structure of a basic PHP project. Additionally, it provides a brief history of PHP and its current usage in the industry, emphasizing its significance in creating dynamic web applications.

Uploaded by

Jude Solis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction to PHP Basics

This document is a guide to the basics of PHP, covering its role in web development, syntax, variables, and basic functions. It outlines the materials needed for learning, the installation of local web servers like XAMPP and WAMP, and the structure of a basic PHP project. Additionally, it provides a brief history of PHP and its current usage in the industry, emphasizing its significance in creating dynamic web applications.

Uploaded by

Jude Solis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Introduction

to PHP Basics
A QUICK INFORMATION GUIDE
Objectives
STUDENTS WILL LEARN THE FUNDAMENTALS OF
PHP, INCLUDING SYNTAX, VARIABLES, AND BASIC
FUNCTIONS, AND WILL CREATE A SIMPLE DYNAMIC
WEB PAGE USING PHP.
Materials Needed:
·Computers with internet
access

·Code editor (e.g., Visual


Studio Code)

·Web server (e.g., XAMPP,


WAMP, or a live server)
Lesson Outline
1.Introduction to PHP

What is PHP’s role in web development?

-PHP, which stands for Hypertext Preprocessor, is a


widely-used open-source server-side scripting language
designed specifically for web development12.
Lesson Outline
1.Introduction to PHP

What is PHP?

Server-Side Scripting Language: PHP code is executed on


the server, and the result is sent to the client’s web
browser as plain HTML.
Open Source: PHP is free to use and has a large
community of developers who contribute to its continuous
improvement.
Roles of PHP in Web Development:

1. 2. 3. 4. 5.
DYNAMIC FORM DATABASE SESSION FILE
CONTENT HANDLING INTERACTION MANAGEMENT OPERATIONS
GENERATION

PHP can generate It can collect data from PHP can interact with It can manage user PHP can create, open, read,
dynamic page content, forms, process it, and various databases like sessions, which is write, delete, and close files
allowing websites to store it in databases, MySQL, PostgreSQL, crucial for maintaining on the server, which is
display different making it essential for and Oracle, enabling user state and data useful for tasks like file
content based on user creating interactive the creation of data- across multiple pages uploads and downloads1.
interactions or other web applications. driven websites. (e.g., user login Security: PHP includes
variables. systems). features for encrypting data
and managing user access,
helping to secure web
applications.
The difference between client-side and server-side
scripting.
-Client-Side Scripting
Execution Location: Runs on the user’s browser.
Languages Used: Commonly uses HTML, CSS, and JavaScript.
Purpose: Enhances user interfaces and interactions directly in the browser without needing to
communicate with the server for every action.
Visibility: Source code is visible to the user.
Performance: Can reduce load on the server by handling tasks like form validation, animations, and
dynamic content updates on the client side.
Security: Less secure since the code is exposed to the user.
Examples: Validating form inputs, creating interactive elements like sliders and carousels, and updating
content dynamically without refreshing the page.
-Server-Side Scripting
Execution Location: Runs on the web server.
Languages Used: Commonly uses PHP, Python, Ruby, Java, and Node.js.
Purpose: Handles data processing, database interactions, and dynamic page generation on the server.
Visibility: Source code is not visible to the user; only the output (HTML) is sent to the user’s browser.
Performance: Can handle complex tasks and heavy computations, but may increase server load.
Security: More secure as the code and data processing happen on the server.
Examples: Managing user authentication, accessing and manipulating databases, and generating dynamic
content based on user requests.

-Key Differences
Execution: Client-side scripts run on the user’s device, while server-side scripts run on the server.
Visibility: Client-side code is visible to users, whereas server-side code is hidden.
Interactivity: Client-side scripting allows for more immediate interactivity without server communication while
server-side scripting is essential for tasks requiring server resources and data.
Brief history and current
use of PHP in the industry.
Learning sessions are now conducted online in 95% of schools in the world.
-PHP, originally created by Rasmus Lerdorf in 1994, started as a set of Common Gateway
Interface (CGI) binaries written in C to track visits to his online resume. Initially named
“Personal Home Page Tools” (PHP Tools), it evolved significantly over time. By 1995,
Lerdorf had released the source code to the public, allowing for community contributions
and improvements. This led to the development of PHP/FI (Forms Interpreter), which
included basic functionality for dynamic web applications.

-In 1997, PHP 3.0 was released, marking a significant milestone as it introduced a more
robust and extensible architecture. PHP 4.0, released in 2000, brought the Zend Engine,
which greatly improved performance. PHP 5.0, released in 2004, introduced object-oriented
programming features. The language continued to evolve, with PHP 7.0 in 2015 bringing
major performance enhancements and PHP 8.0 in 2020 introducing the Just-In-Time (JIT)
compiler.
Current Use of PHP in
22.1%

the Industry
DESPITE THE EMERGENCE OF NUMEROUS NEW PROGRAMMING
LANGUAGES AND FRAMEWORKS, PHP REMAINS A DOMINANT FORCE IN
WEB DEVELOPMENT. AS OF 2023, PHP IS USED BY APPROXIMATELY
77.5% OF ALL WEBSITES WHOSE SERVER-SIDE PROGRAMMING
LANGUAGE IS KNOWN. THIS INCLUDES MAJOR PLATFORMS LIKE
77.9%
FACEBOOK, WORDPRESS, AND WIKIPEDIA.

PHP is particularly popular for developing content management systems (CMS) such as WordPress, Drupal, and
Joomla. Its ease of use, extensive documentation, and large community support make it a preferred choice for
many developers. Additionally, PHP’s compatibility with various databases and its ability to run on almost any
server and operating system contribute to its widespread adoption.

PHP continues to be relevant due to its constant updates and improvements, ensuring it meets modern web
development needs. The introduction of PHP 8.0 with the JIT compiler has further enhanced its performance,
making it competitive with newer languages.
Lesson Outline
2. Setting up the Environment

A Guide through installing a local web server


like XAMPP or WAMP.
Installing XAMPP

1 DOWNLOAD XAMPP 2 RUN THE INSTALLER 3 INSTALLATION SETUP


Come up with an educated guess based Locate the downloaded file and Follow the setup wizard. You can
on your research.Visit the official XAMPP choose the components you want to
double-click to run the installer.
website. install. For a basic setup, the default
If prompted by User Account Control
selection (Apache, MySQL, PHP, and
Choose the version compatible with your (UAC), click “Yes” to allow the installer phpMyAdmin) is sufficient.
operating system (Windows, macOS, or to make changes to your device. Choose the installation directory (the
Linux) and download the installer.
default is usually C:\xampp).

4 COMPLETE THE 5 START THE SERVICES 6 VERIFY THE INSTALLATION


INSTALLATION Open the XAMPP Control Panel. Open your web browser and type
Click “Next” and then “Finish” to Start the Apache and MySQL services http://localhost in the address bar.
complete the installation. You can by clicking the “Start” buttons next to You should see the XAMPP welcome
choose to start the XAMPP Control each. page, indicating that your local server is
Panel immediately. running correctly.
Installing WAMP
1 2 3 4
STEP STEP STEP STEP
Download WAMP Run the Installer Start the Verify the
Services Installation
Visit the official Locate the
WAMP website. downloaded file After installation, Open your web
Download the and double-click to launch WAMP from browser and type
installer for your run the installer. the Start menu or http://localhost in the
operating system. Follow the setup desktop shortcut. address bar.
wizard, choosing The WAMP icon in You should see the
the default options the system tray WAMP server
unless you have should turn green, configuration page.
specific indicating that the
preferences. services are
running.
Lesson Outline
2. Setting up the Environment

Learn to set up a basic PHP


project structure.
1

Project Root Directory


THIS IS THE MAIN FOLDER FOR YOUR PROJECT.

index.php: The main entry point config.php: Configuration htaccess: Apache configuration
for your application. settings for your project. file (if you’re using Apache).
Directories
2 EXAMPLE STRUCTURE
assets/: Contains static files like CSS, JavaScript, and images.
css/: Stylesheets.
js/: JavaScript files.
images/: Image files.

includes/: Contains reusable PHP scripts.


header.php: Common header file.

footer.php: Common footer file.


classes/: Contains class definitions.
functions/: Contains custom functions
templates/: Contains template files for different parts of your site.
uploads/: Directory for uploaded files.
3

PHP Syntax and Basics

Basic PHP syntax: <?php ... ? Echoing output: echo and Comments in PHP: single-line (//)
>. print. and multi-line (/* ... */).
4

Variables and Data Types

Declaring variables: Data types: strings, integers, Basic operations with


$variable_name floats, booleans, arrays. variables.
5

Basic PHP Functions

Common built-in functions:


Introduction to functions: defining strlen(), str_replace(),
and calling functions. array_push(). Creating user-defined functions
PHP Tutorial

PHP IS A SERVER SCRIPTING


LANGUAGE, AND A POWERFUL TOOL FOR
MAKING DYNAMIC AND INTERACTIVE
WEB PAGES.

PHP IS A WIDELY-USED, FREE, AND


EFFICIENT ALTERNATIVE TO
COMPETITORS SUCH AS MICROSOFT'S
ASP.
What is PHP
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
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

HTML
IS THE STANDARD MARKUP LANGUAGE
FOR WEB PAGES.
WITH HTML YOU CAN CREATE YOUR
OWN WEBSITE.
HTML IS EASY TO LEARN - YOU WILL
ENJOY IT!
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

CSS
IS THE LANGUAGE WE USE TO STYLE AN
HTML DOCUMENT.
CSS DESCRIBES HOW HTML ELEMENTS
SHOULD BE DISPLAYED.
THIS TUTORIAL WILL TEACH YOU CSS
FROM BASIC TO ADVANCED.
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

JavaScript
IT IS THE WORLD'S MOST POPULAR PROGRAMMING LANGUAGE.

JAVASCRIPT IS THE PROGRAMMING LANGUAGE OF THE WEB.


JAVASCRIPT IS EASY TO LEARN.
THIS TUTORIAL WILL TEACH YOU JAVASCRIPT FROM BASIC TO ADVANCED.
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

PHP Installation
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
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

Use a Web Host With PHP Support


IF YOUR SERVER HAS ACTIVATED SUPPORT FOR PHP YOU DO NOT NEED TO DO ANYTHING.

JUST CREATE SOME .PHP FILES, PLACE THEM IN YOUR WEB DIRECTORY, AND THE SERVER
WILL AUTOMATICALLY PARSE THEM FOR YOU.
YOU DO NOT NEED TO COMPILE ANYTHING OR INSTALL ANY EXTRA TOOLS.
BECAUSE PHP IS FREE, MOST WEB HOSTS OFFER PHP SUPPORT.
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

PHP Online Compiler / Editor


WITH W3SCHOOLS' ONLINE PHP COMPILER, YOU CAN EDIT PHP CODE, AND VIEW THE
RESULT IN YOUR BROWSER.

PHP Syntax
A PHP SCRIPT IS EXECUTED ON THE SERVER, AND THE PLAIN HTML RESULT IS SENT BACK
TO THE BROWSER.
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

Basic PHP Syntax


-A PHP SCRIPT CAN BE PLACED ANYWHERE IN THE DOCUMENT.
-A PHP SCRIPT STARTS WITH <?PHP AND ENDS WITH ?>:
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

Basic PHP Syntax


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:

NOTE: PHP STATEMENTS END WITH A SEMICOLON (;).


BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

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:

EXAMPLE
ECHO IS THE SAME AS ECHO:

NOTE: HOWEVER; ALL


VARIABLE NAMES ARE
CASE-SENSITIVE!
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

PHP Case Sensitivity


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:

EXAMPLE
$COLOR IS NOT SAME AS $COLOR:
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

PHP Comments
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
· LEAVE OUT SOME PARTS OF YOUR CODE
PHP SUPPORTS SEVERAL WAYS OF COMMENTING:
Example: Syntax for comments in
PHP code:
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

Single Line Comments


SINGLE LINE COMMENTS START WITH //.
ANY TEXT BETWEEN // AND THE END OF THE LINE WILL BE IGNORED (WILL NOT BE EXECUTED).
YOU CAN ALSO USE # FOR SINGLE LINE COMMENTS, BUT IN THIS TUTORIAL WE WILL USE //.
THE FOLLOWING EXAMPLES USES A SINGLE-LINE COMMENT AS AN EXPLANATION:

EXAMPLE
A COMMENT BEFORE THE CODE:

EXAMPLE
A COMMENT AT THE END OF A
LINE:
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

Multi-Line Comments
MULTI-LINE COMMENTS START WITH /* AND END WITH */.
ANY TEXT BETWEEN /* AND */ WILL BE IGNORED.
THE FOLLOWING EXAMPLE USES A MULTI-LINE COMMENT AS AN EXPLANATION:

EXAMPLE:
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

Comments in the Middle of the Code


THE MULTI-LINE COMMENT SYNTAX CAN ALSO BE USED TO PREVENT EXECUTION OF PARTS INSIDE A
CODE-LINE:

EXAMPLE
THE + 15 PART WILL BE IGNORED IN THE CALCULATION:
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

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:

IN THE EXAMPLE ABOVE, THE VARIABLE $X WILL HOLD THE VALUE 5, AND THE VARIABLE $Y WILL HOLD THE
VALUE "JOHN".
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.
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

PHP Variables
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)
BEFORE YOU CONTINUE YOU SHOULD HAVE A BASIC UNDERSTANDING OF THE FOLLOWING:

PHP Data Types


VARIABLES CAN STORE DATA OF DIFFERENT TYPES, AND DIFFERENT DATA TYPES CAN DO DIFFERENT
THINGS.

PHP SUPPORTS THE FOLLOWING DATA TYPES:


·STRING
·INTEGER
·FLOAT (FLOATING POINT NUMBERS - ALSO CALLED DOUBLE)
·BOOLEAN
·ARRAY
·OBJECT
·NULL
·RESOURCE
Activity: Create a Simple Contact Form
Activity: Create a Simple
Contact Form
Build a basic contact form using PHP to understand how to handle
form submissions and validate user input.

1. SET UP THE ENVIRONMENT


INSTALL AND CONFIGURE YOUR LOCAL SERVER ENVIRONMENT.
Activity: Create a Simple
Contact Form
2. CREATE THE HTML FORM CREATE AN
INDEX.PHP FILE IN YOUR PROJECT FOLDER.
Activity: Create a Simple
Contact Form
3.HANDLE FORM SUBMISSION
CREATE A PROCESS_FORM.PHP FILE TO
HANDLE FORM SUBMISSIONS
Activity: Create a Simple
Contact Form
4. TEST THE FORM
START YOUR LOCAL SERVER AND NAVIGATE TO
HTTP://LOCALHOST/YOUR_PROJECT_FOLDER/INDEX.PHP.
FILL OUT THE FORM AND SUBMIT IT TO SEE THE RESULTS.

5. ENHANCE THE APPLICATION


ADD MORE VALIDATION (E.G., EMAIL FORMAT CHECK).
STYLE THE FORM WITH CSS TO MAKE IT MORE VISUALLY APPEALING.
SAVE THE FORM DATA TO A FILE OR DATABASE FOR FUTURE REFERENCE
THANK YOU!

You might also like