IT16
Integrative
Programming
& Technologies 2
Introduction to PHP
Prepared by: Daryl Factor
Discussion:
• About PHP
• Tools needed to start local development
• Setting up working local environment
• Basics of PHP
• Syntax, Tags, Comments, & Concatenation
• Declaring Variables
• Constants, Magic Constants, & Predefined Constants
• Displaying Outputs
What is PHP?
• PHP is a server scripting language, and a powerful tool for making dynamic
and interactive web pages.
• PHP originally an acronym for “Personal Home Page”.
• But now stands for “PHP: Hypertext Preprocessor”.
• PHP was created by Danish-Canadian programmer Rasmus Lerdorf in 1994.
• PHP is a widely-used, open-source scripting language.
• PHP is free to download and use.
Evolution of PHP
• 1994 – Creation
• Developed by Rasmus Lerdorf as a set of CGI scripts to track visits to his homepage.
• Originally called Personal Home Page Tools.
• 1995 – PHP/FI (Forms Interpreter)
• Released as open-source, enabling dynamic web forms and database interaction.
• 1997 – PHP 3
• Rewritten by Zeev Suraski and Andi Gutmans.
• Reason: PHP/FI had limitations in performance and scalability for larger projects.
• Introduced modular architecture and database support.
• PHP 3 was the first widely accepted version used for commercial and enterprise-level applications.
• Renamed to PHP: Hypertext Preprocessor.
Evolution of PHP
• 2000 – PHP 4
• Based on Zend Engine 1.0, developed by Zeev Suraski and Andi Gutmans.
• Improved performance and scalability.
• Widely used in early 2000s websites.
• 2004 – PHP 5
• Introduced Object-Oriented Programming, PDO, and better error handling.
• Used in major platforms like WordPress and Facebook.
• PHP became suitable for enterprise applications and frameworks like Laravel, Symfony, and Zend
Framework.
• 2005–2010 – PHP 6 (Unreleased)
• Attempted Unicode support; eventually failed and was abandoned.
• The project failed due to complexities in implementation.
Evolution of PHP
• 2015 – PHP 7
• Skipped version 6 due to confusion.
• Major performance boost, type declarations, new operators.
• Solidified PHP’s place in modern web development, competing strongly with Node.js, Python, and
Ruby.
• 2020 – PHP 8
• Introduced JIT (Just-In-Time) compiler, attributes, named arguments, and more modern features.
• Faster execution, cleaner syntax, and modern programming features.
• 2021–Present – PHP 8.1, 8.2, 8.3+
• Continued focus on performance, security, and modern programming paradigms.
• Added enums, readonly properties, union/intersection types, and deprecation of legacy features.
• Widely used in CMS platforms (WordPress, Drupal), eCommerce systems (Magento, WooCommerce),
and modern frameworks (Laravel, Symfony).
Evolution of PHP
• 2025 – Today
• Powers ~76% of the web (e.g., WordPress, Facebook, Wikipedia).
• Actively maintained and modernized to support secure, fast, and scalable web apps.
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 connect to your database.
• PHP can add, delete, modify data in your database.
• PHP can be used to control user-access.
• PHP can encrypt data.
What is a PHP file?
• PHP files can contain text, HTML, CSS, JavaScript, and PHP code.
• PHP code are executed on the server, and the result is returned to the browser
as plain HTML.
• PHP files have an extension of “.php”
What are the websites developed in PHP?
• According to chudovo.com these are the Top 10 Websites Developed in PHP by
2024:
• Facebook • Etsy
• Wikipedia • WordPress
• Tumblr • Pfizer
• Slack • Canva
• MailChimp • Spotify
What are the basic knowledge needed?
• Before you continue you should have a basic understanding of the ff.:
• HTML, CSS, & JavaScript
• HTML, CSS, and JavaScript will work on front-end side for the layout, styles,
and animation.
• While in PHP, it will work on the back-end side for the form validation, data
sanitization, and the saving of data into the database.
What are the things needed to start our local
development?
• Install a web server, such as Apache
• Install PHP
• Install a database, such as MySQL
• Install a text editor
• Install a browser
Do we have any installer package to set up our local
development server?
• YES!! And we call it..
• Web Server Solution Package
XAMPP WAMP LAMP
What are the source code editors that we can use?
SUBLIME TEXT NOTEPAD++ VISUAL STUDIO CODE
What are the browsers that we can use?
GOOGLE MOZILLA MICROSOFT
CHROME FIREFOX EDGE
Setting up a Local Server using XAMPP
• Latest Version: 8.2.12
• 1. Download XAMPP 8.2.12
• Visit the official Apache Friends website: https://www.apachefriends.org
• Select XAMPP version 8.2.12 for your operating system (Windows, macOS, or Linux).
• Click the Download button.
• 2. Run the Installer
• Locate the downloaded file (e.g., xampp-windows-x64-8.2.12-0-VS16-installer.exe on Windows).
• Right-click and select "Run as administrator" (Windows only) to avoid permission issues.
Setting up a Local Server using XAMPP
• 3. Allow UAC (Windows Only)
• If prompted by User Account Control (UAC), click Yes to proceed.
• 4. Start XAMPP Setup Wizard
• The XAMPP Setup Wizard will open.
• Click Next.
• 5. Select Components
• Choose the components to install:
• Recommended: Apache, MySQL, PHP, phpMyAdmin.
• Optional: FileZilla FTP Server, Mercury Mail Server, Tomcat, Perl, etc.
• Click Next.
Setting up a Local Server using XAMPP
• 6. Choose Installation Folder
• Default: C:\xampp (Windows) or /Applications/XAMPP (macOS).
• It's best to leave the default location.
• Click Next.
• 7. Bitnami for XAMPP (Optional)
• You'll see a prompt for Bitnami for XAMPP (offers WordPress, Drupal, Joomla installers).
• Uncheck if you don't need it.
• Click Next.
Setting up a Local Server using XAMPP
• 8. Start Installation
• Click Next, then wait for the installation process to complete.
• 9. Complete Setup
• Once installation finishes, leave the option "Do you want to start the Control Panel now?" checked.
• Click Finish.
• 10. Open XAMPP Control Panel
• The XAMPP Control Panel will appear.
• Start the services:
• Click Start next to Apache (for PHP).
• Click Start next to MySQL (for database).
• The background should turn green if they run successfully.
Setting up a Local Server using XAMPP
• 11. Test XAMPP Installation
• Open your browser and go to: 👉 http://localhost/
• You should see the XAMPP Dashboard.
• 12. Verify PHP Version
• To confirm PHP version 8.2.12:
• Create a file named info.php inside C:\xampp\htdocs
• Add: <?php phpinfo(); ?>
• Open http://localhost/info.php in your browser.
PHP Syntax
• ✅ Basic PHP Syntax
• Key Rules:
• PHP statements end with a semicolon ;
• PHP code is embedded within PHP tags (<?php ... ?>)
• PHP is case-insensitive for keywords (echo, Echo, ECHO all work)
• Variables are case-sensitive ($name ≠ $Name).
PHP Tags
• ✅ Standard PHP Tag (Recommended)
• ✅ Short Echo Tag (used for output)
• ⚠️Not Recommended: Short Open Tag
PHP Comments
• Comments are used to document code and are ignored by the PHP interpreter.
• ✅ Single-line Comments
• ✅ Multi-line Comment
PHP Variables
• ✅ Syntax
• Start with $
• Must begin with a letter or underscore
• Case-sensitive
• ❌ Invalid Variable Names
PHP Constants
• In PHP, a constant is a name (identifier) for a simple value that cannot be changed during the
execution of the script.
• Once you define a constant, its value stays the same throughout your program.
Key Differences Between Constants and Variables
Feature Variable Constant
Declaration Uses $ (e.g., $name) No $ (e.g., SITE_NAME)
Changeable Value can be changed Value cannot be changed
Follows variable scope rules (local,
Scope Global scope by default
global, static)
Constants are case-sensitive (unless
Case Sensitivity Variables are case-sensitive specified otherwise in older PHP
versions)
Defining Constants
• PHP offers two main ways to define constants:
• Using define()
• Syntax: define(name, value)
• name: The name of the constant (string).
• value: The constant value.
Defining Constants
• PHP offers two main ways to define constants:
• Using const keyword
• Must be declared at the top level of a script or inside classes/interfaces.
• Cannot be used inside functions to create constants dynamically.
• Slightly faster than define() because it's a language construct.
Rules for Naming Constants
• Can contain letters, numbers, and underscores.
• Cannot start with a number.
• No $ sign at the start.
• Typically written in uppercase for readability (MAX_USERS, DB_HOST).
Accessing Constants
• Since constants are global by default, they can be accessed:
• Anywhere in the script
• Inside functions, classes, and files without using global keyword.
Magic Constants in PHP
• Magic constants are built-in constants that change depending on where they are used in the script.
• They start and end with two underscores ( __ ).
Magic Constant Description Example Output
__LINE__ Current line number in the file 15
__FILE__ Full path and filename of the file /var/www/html/index.php
__DIR__ Directory of the file /var/www/html
__FUNCTION__ Name of the current function myFunction
__CLASS__ Name of the current class (with namespace) MyClass
__TRAIT__ Name of the current trait MyTrait
__METHOD__ Name of the current class method MyClass::myMethod
__NAMESPACE__ Current namespace name MyNamespace
Predefined Constants
• Predefined constants are constants already defined by PHP that give useful information about the PHP
environment, configuration, and limits.
a. PHP Environment Constants
Constant Description Example Output
PHP_VERSION Current PHP version 8.2.12
PHP_OS Operating system PHP is running on Linux
PHP_SAPI Server API used by PHP apache2handler
PHP_INT_MAX Largest integer supported 9223372036854775807
PHP_INT_MIN Smallest integer supported -9223372036854775808
PHP_INT_SIZE Size of an integer in bytes 8
All errors and warnings reporting
E_ALL 32767
level
b. File System Constants
Constant Description
OS-specific directory separator (/ for Linux, \ for
DIRECTORY_SEPARATOR
Windows)
PATH_SEPARATOR Separator for paths (: for Linux, ; for Windows)
PHP_EOL End of line character (depends on OS)
Checking if a Constant is Defined
When to Use Constants
• Database connection details:
• Application settings:
Summary
• Constants: Unchangeable, global values in PHP.
• Define with: define() or const.
• Magic Constants: Change based on location in code.
• Predefined Constants: Provided by PHP to get environment info.
• Best Practice: Use constants for fixed values like configuration, not for
values that may change during runtime.
PHP Displaying Output
• ✅ Using echo (most common)
• ✅ Using print (returns 1, slightly slower)
• ✅ Using print_r() (good for arrays)
• ✅ Using var_dump() (shows data type and value)
PHP Concatenation
• Concatenation is combining strings using the dot (.) operator.
• ✅ Example:
• ✅ Shortcut with Concatenation Assignment
Thank you!