PHP Unit1 Prep

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

UNIT - 1

Introduction to PHP
PHP (Hypertext Preprocessor) is a server-side scripting language
designed for web development but can also be used as a
general-purpose programming language. It is embedded in HTML and
works with web servers to create dynamic web pages.

or

PHP (Hypertext Preprocessor) is a server-side scripting language


mainly used for web development. It creates dynamic web pages that
interact with databases or handle user input. Unlike HTML (which is
static), PHP generates content dynamically.

Why is it popular?

● Easy to Learn: Syntax is straightforward.


● Free & Open Source: No cost.
● Cross-platform: Works on Windows, Linux, etc.
● Web-Focused: Designed for creating interactive websites.
Evolution of PHP
● PHP 1.0 (1995): Initially named "Personal Home Page Tools"
by Rasmus Lerdorf for tracking visits.
● PHP 2.0 (1997): Added basic features like form handling.
● PHP 3.0 (1998): Officially became PHP: Hypertext
Preprocessor.
● PHP 4.0 (2000): Introduced the Zend Engine.
● PHP 5.0 (2004): Object-Oriented Programming (OOP)
support.
● PHP 7.0 (2015): Performance boost with a new engine.
● PHP 8.0 (2020): JIT (Just-In-Time) compilation for faster
performance

Interfaces to External Systems


WTF does it even means ?
When we say “interfaces to external systems”, it’s all about how
PHP communicates and works with other systems that aren’t part of
PHP itself. Think of it as PHP being a middleman that connects your
web application to external tools, services, or hardware.

An interface in this context means a way or method through which


PHP can:

● Fetch or send data.


● Perform tasks using external tools.
● Integrate with other software, databases, or systems.

Think of it like this:


PHP = A smartphone.
External systems = Apps and the internet.
By interfacing with apps and online services, your phone becomes way
more useful. Similarly, PHP’s ability to connect with other systems
makes it powerful for real-world applications.

Following are some examples:

PHP plays nicely with a lot of external systems. Here are some key
ways it integrates:

1. Database Systems

PHP is known for its seamless integration with databases like:

● MySQL (most common)


● PostgreSQL
● SQLite
2. APIs

PHP can interact with external APIs to fetch or send data. For example:

● Weather APIs
● Payment gateways (e.g., PayPal, Stripe)

If you are wondering WTF is an API ?

An API (Application Programming Interface) is like a waiter in a


restaurant, bruh.

Imagine you’re at a restaurant (your app) and you wanna order some
food (data/functionality) from the kitchen (another app/system). You
don’t go into the kitchen yourself, right? You tell the waiter (API) what
you want, and they bring it to you.

Basically, an API is the middleman that lets different software talk to


each other, exchange data, and do cool sh*t without exposing all their
internal secrets.
3. File Systems

You can use PHP to read, write, or manipulate files.

4. Email Systems

PHP has built-in support for sending emails.

Hardware Requirements for PHP


Any standard computer (1GB+ RAM for practice).

● Processor: Basic (Intel i3 or better).


● RAM: Minimum 1 GB, recommended 4 GB+.
● Storage: 10 GB free (SSD recommended).
● Network: Basic internet connection.

Software Requirements for PHP


1. Operating System: Windows, macOS, or Linux.
2. PHP: Latest version.
3. Web Server: Apache (common) or Nginx.
4. Database: MySQL or MariaDB.
5. Text Editor: VS Code, Sublime Text, or PHPStorm.
6. Optional: Composer (dependency manager) & phpMyAdmin.

PHP Scripting
PHP scripting refers to writing PHP code that runs on a web server to
create dynamic web pages or handle server-side tasks.

● PHP code is embedded in HTML and executed on the server.


● The result is sent to the user’s browser as plain HTML.

Basic PHP Development


To develop in PHP, you need:

1. PHP installed (or a local server like XAMPP/WAMP).


2. Write PHP code in a .php file and run it on a server.
3. Use a browser to view the results.
Working of PHP Scripts
1. Request: User requests a .php file by visiting the URL.
2. Execution: PHP processes the script on the server.
3. Output: The server sends the HTML output to the browser.

Basic PHP Syntax


● PHP code is enclosed in <?php ... ?>.
● Statements end with a semicolon (;).
Following are PHP Data Types:

String: A sequence of characters (text) enclosed in quotes, like names


or sentences.

Integer: Whole numbers (without decimal points), like 1, 100, or -50.

Float (Double): Numbers with decimal points, used for precise


calculations, like 3.14 or -0.99.

Boolean: Represents one of two values: true or false. Used for


logical conditions.
Array: A collection of multiple values stored in a single variable, which
can be accessed via an index or a key.

Object: An instance of a class containing properties (variables) and


methods (functions) to represent real-world entities.
NULL: A special variable that represents no value or an empty state. A
variable with no value assigned is null.
In PHP, you can display type information or test for a specific data
type using built-in functions like gettype(), var_dump(), and is_*
functions.

Here’s a breakdown of how to do both:

1. Displaying Type Information


To display the data type of a variable, you can use:

● gettype(): Returns the type of the variable.


● var_dump(): Displays detailed information about the variable,
including its type and value.
Explanation:

● gettype() returns the data type as a string (e.g., "string",


"integer", "double").
● var_dump() provides more detailed information, such as the type
and the length/size of the value.

2. Testing for a Specific Data Type


To test if a variable is of a specific data type, you can use the is_*
functions like is_string(), is_int(), is_float(), etc.
Explanation:

● is_string() checks if a variable is a string.


● is_int() checks if a variable is an integer.
● is_float() checks if a variable is a float.
● is_bool() checks if a variable is a boolean.

Common is_* Functions:

● is_string($var) – Checks if the variable is a string.


● is_int($var) – Checks if the variable is an integer.
● is_float($var) – Checks if the variable is a float.
● is_bool($var) – Checks if the variable is a boolean.
● is_array($var) – Checks if the variable is an array.
● is_object($var) – Checks if the variable is an object.
● is_null($var) – Checks if the variable is NULL.

By using these functions, you can easily check the type of a variable
and output or process data accordingly in PHP!

CHANGING TYPE WITH SET TYPE

In PHP, you can change the type of a variable using type casting or
type coercion.

There are two main ways to change a variable's type:

1. Implicit Type Conversion (Type Coercion)


PHP automatically converts a variable to the required type depending
on the context (this is known as type coercion). For example, if you
perform an operation that requires a different type, PHP will
automatically convert the variable.

Example of Implicit Type Conversion:

Explanation: In the above example, $number is a string, but when you


add it to 10, PHP automatically converts the string to an integer, so
the result is 110.

2. Explicit Type Casting (Set Type)


You can explicitly cast a variable to a specific type using (type) syntax.
This is known as explicit type casting.

Example of Explicit Type Casting:

Explanation:
● (int) casts the string "85.5" to an integer, so the value
becomes 85.
● (float) casts the string "85.5" to a float, keeping the decimal.

3. Using Functions to Change Type


In PHP, there are also functions like intval(), floatval(),
strval() to explicitly convert between types.

Example using intval(), floatval(), strval():

Explanation:

● intval() converts the value to an integer.


● floatval() converts the value to a float.
● strval() converts the value to a string.
Type Casting Summary:
● Implicit: PHP automatically converts the type when necessary
(e.g., adding a string and an integer).
● Explicit: You force the conversion using type casting or
functions ((int), (float), intval(), floatval()).

So if you want to force a change, you use explicit casting, but PHP
will do it for you automatically in many cases with implicit conversion.

1. Operators in PHP
Operators are symbols that perform operations on variables and
values. They can be classified into several types:

● Arithmetic Operators: Used to perform mathematical operations.


○ +, -, *, /, % (addition, subtraction, multiplication, division,
modulus)
● Example:

Assignment Operators: Used to assign values to variables.

● =, +=, -=, *=, /=, etc.


Example:

Comparison Operators: Used to compare two values.

● ==, ===, !=, >, <, >=, <=

Example:

Logical Operators: Used to perform logical operations.

● &&, ||, ! (AND, OR, NOT)

Example:
2. Variable Manipulation
Variable manipulation refers to modifying or operating on variables
directly, often using the operators mentioned above.

In this example:

● The integer $power is manipulated by adding 200 to it.


● The string $hero is updated by concatenating more text.

3. Dynamic Variables
Dynamic variables are variables whose names are determined during
the program's execution. You can create variables with dynamic names
using the variable variables feature.
Here, $$varName is a dynamic variable. The value of $varName
(heroName) becomes the name of the new variable, and it is assigned
the value "Iron Man".

4. Variable Scope
Variable scope refers to the context in which a variable is accessible. In
PHP, there are three main types of variable scope:

● Local Scope: Variables declared inside a function or a block are


local to that function/block and can’t be accessed outside of it.
Example:
Global Scope: Variables declared outside any function are global and
can be accessed anywhere outside functions, but not directly inside
functions (unless passed or made global).

Example:

Static Scope: A variable declared as static inside a function retains


its value between function calls.

Example:

n this example, the static variable $count preserves its value


between calls to the countCalls() function.
Summary of Concepts
● Operators: Perform operations like addition, subtraction,
comparison, etc.
● Variable Manipulation: Changing values of variables using
operators.
● Dynamic Variables: Variables whose names are created
dynamically using other variables.
● Variable Scope: The context in which a variable is accessible
(local, global, static).

You might also like