PHP Programming Notes
Introduction to PHP
- PHP stands for "PHP: Hypertext Preprocessor".
- Open source scripting language, widely-used, and freely available to download.
- Server required to execute PHP scripts.
PHP Overview
- Rasmus Lerdorf released the first version in 1994.
- Server-side scripting language embedded in HTML.
- Capable of managing dynamic content, databases, session tracking, and e-commerce applications.
- Integrates with popular databases like MySQL, PostgreSQL, and Oracle.
- Known for fast execution, especially as an Apache module on Unix.
- Supports protocols like POP3, IMAP, and LDAP.
Common Uses of PHP
- Perform system functions: Create, open, read, write, and close files.
- Handle forms: Collect data from users, save to files, and send data over email.
- Database manipulation: Add, delete, modify elements in databases, access cookies, and restrict user access to pages.
Installation of PHP and MySQL
Starting PHP
- Options to start PHP:
- Find a web host with PHP and MySQL support.
- Install a web server and database on PC.
Web Host with PHP Support
- Create .php files and store them in the web directory; the server parses them automatically.
Setting up PHP on Personal Computer
- If the server does not support PHP:
- Install a web server and PHP.
- Set up a database (e.g., MySQL).
Configuration Recommendations
- Check and configure IIS and PHP settings via the "php.ini" file.
- Use PHP Manager for IIS to set attributes for security.
Features of PHP
- Open Source: Free to use with no licensing fees.
- Extensive Library: Access to optimized code libraries for various tasks.
- Adaptable: Works with various platforms (Windows, Linux, etc.).
- Community Support: Large community for help and resources.
- Easy Maintenance: Framework assistance (like MVC) for managing code.
Advantages of PHP
- Scalable: Used for large projects (e.g., Facebook, WordPress).
- Free to Use: Popular for server-side scripting without cost.
- Ease of Use: Simple to learn due to forgiving syntax.
- Lightning Speed: Fast execution compared to other languages (e.g., three times faster than Python).
PHP Code Writing
Basic Syntax
- Use
<?phpto open and?>to close PHP scripts. - Default file extension for PHP files is ".php".
- Example for outputting text:
<?php
echo "Hello World!";
?>
Executing PHP and Viewing in Browser
- PHP keywords and built-in functions are not case-sensitive (e.g.,
echois the same asECHO). - Variables in PHP are case-sensitive.
Example:
<?php
$color = "red";
echo "My car is " . $color . "!";
?>
Data Types in PHP
- Supported data types include:
- String
- Integer
- Float
- Boolean
- Array
- Object
- NULL
- Resource
PHP Strings
- A string is a sequence of characters.
- Can be created with single quotes ('') or double quotes ("").
- Example:
$str = "Hello, World!";
echo $str;
PHP Integers
- Integer ranges from -2,147,483,648 to 2,147,483,647.
- Can be decimal, hexadecimal, octal, or binary.
PHP Floats
- Float is a number with a decimal point.
Operators in PHP
- Arithmetic Operators: +, -, *, /, %
- Comparison Operators: ==, !=, ===, !==
- Logical Operators: and, or, &&, ||
Control Structures and Functions
Control Structures
- If statement: Evaluate conditions for code execution.
- Switch statement: Perform different actions based on a variable's value.
PHP Functions
- User-defined functions are custom functions created for specific tasks.
- Built-in functions are pre-available and do not require additional coding.
- Examples include
strlen(),count(),date(), etc.
- Examples include
Creating Functions:
To define a function:
function functionName() {
// code to execute
}
Call the function using:
functionName();