PHP Overview PHP, or PHP: Hypertext Preprocessor, is a widely-used open-source server-side scripting language that is particularly suited for web development. Let's delve into the various aspects of PHP and learn about its capabilities.
PHP was created by Rasmus Lerdorf in 1994. Originally designed to manage his online resume and count visitors, it evolved over time to become one of the primary languages for web development. As of this writing, the latest stable version is PHP 8.
PHP scripts can be embedded within HTML, enabling the server to execute PHP code to generate dynamic content. A PHP file typically uses the `.php` extension.
Example:
<!DOCTYPE html>
<html>
<title>PHP Test</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
PHP variables start with the `$` sign, followed by the variable name. They don't need to be declared before using them.
Example:
<?php
$name = "John";
$age = 25;
echo "My name is $name and I am $age years old.";
?>
PHP supports multiple data types, including:
- Integers - Floats (or doubles) - Booleans - Strings
- Arrays - Objects
- NULL - Resource Example using arrays:
<?php
$colors = array("Red", "Green", "Blue");
echo $colors[0]; // Outputs "Red"
?>
PHP supports common control structures such as `if`, `else`, `switch`, `while`, and `for`.
Example:
<?php
$score = 85;
if ($score >= 90) {
echo "Excellent!";
} elseif ($score >= 75) {
echo "Very Good!";
} else {
echo "Good!";
}
?>
You can declare custom functions in PHP.
Example:
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice");
?>
PHP can collect form data and process it.
Example: HTML form:
<form method="post" action="welcome.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
Processing in `welcome.php`:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
echo "Welcome, $name! Your email address is $email.";
?>
PHP can connect to databases, most commonly MySQL.
Example:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select data
$sql = "SELECT id, name FROM users";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
$conn->close();
?>
PHP can open, read, write, and close files on the server.
Example:
<?php
$file = fopen("test.txt", "w");
$txt = "Hello, World!\n";
fwrite($file, $txt);
fclose($file);
?>
PHP supports sessions and cookies to store and retrieve user-specific information. Example using sessions:
// Start the session
session_start();
// Set a session variable
$_SESSION["user"] = "Alice";
// Retrieve a session variable
echo "Hello, " . $_SESSION["user"] . "!";
?>
11. Object-Oriented PHP
PHP supports object-oriented programming (OOP) principles such as classes, inheritance, and polymorphism.
Example:
php class Person { public $name; function __construct($name) { $this->name = $name; } function greet() { return "Hello, " . $this->name; } } $john = new Person("John"); echo $john->greet(); ?>
PHP allows you to handle errors gracefully using mechanisms like custom error handlers and exception handling. Example using exceptions: ```php <?php function checkAge($age) { if($age < 18) { throw new Exception("Not an adult."); } return true; } try { checkAge(15); } catch(Exception $e) { echo "Error: " . $e->getMessage(); } ?>
Hence We can say PHP is a versatile and powerful scripting language primarily used for web development. Its dynamic nature allows developers to build robust applications, interact with databases, handle user input, manage sessions, and much more. PHP's open-source nature, strong community, and a wide range of hosting options make it a popular choice for many developers around the world.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here