PHP &mysql
PHP &mysql
Definition of PHP
PHP stands for Hypertext Preprocessor (originally Personal Home Page).
It is an open-source, server-side scripting language designed primarily for web development,
but also used as a general-purpose programming language.
Server-side means PHP scripts are executed on the web server, and the output (usually
HTML) is sent to the client’s browser.
PHP can be embedded within HTML, making it very easy to integrate logic into web
pages.
Example:
<?php
echo "Hello, World!";
?>
Example:
<?php
echo "Welcome to PHP Programming!";
?>
Output:
Basic Structure:
<?php
// This is a PHP script
echo "Hello, Shivani!";
?>
Important Rules:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>PHP Embedded in HTML</h2>
<?php
$name = "Shivani";
echo "<p>Welcome, $name!</p>";
?>
</body>
</html>
Output:
7. Constants in PHP
Constants are like variables, but their values cannot be changed once defined.
Syntax:
Features:
8. Variables in PHP
A variable is used to store data like text or numbers.
Rules:
Start with $.
Must begin with a letter or underscore.
Case-sensitive.
No need to declare data type (PHP is loosely typed).
Example:
<?php
$name = "Shivani";
$age = 21;
echo "Name: $name, Age: $age";
?>
Static Variables
<?php
function counter() {
static $count = 0;
$count++;
echo $count . "<br>";
}
counter();
counter();
counter();
?>
Output:
1
2
3
Global Variables
Declared outside functions and used inside functions with the global keyword.
<?php
$a = 5;
$b = 10;
function sum() {
global $a, $b;
echo $a + $b;
}
sum();
?>
Output:
15
9. Arrays in PHP
An array is a collection of multiple values in a single variable.
Types of Arrays
Multidimensional Array
Arrays inside arrays.
$students = array(
array("Shivani", 95, "A"),
array("Aarya", 89, "B"),
array("Riya", 92, "A")
);
Types of Operators
Type Example Description
Arithmetic + - * / % Basic math operations
Assignment = += -= *= /= Assign or update values
Comparison == != > < >= <= Compare two values
Logical `&&
String . Concatenate strings
Increment/Decrement ++ -- Increase or decrease value
Array + == === != <> !== Compare arrays
Conditional (Ternary) ?: Short form of if-else
Example:
$a = 10;
$b = 5;
echo $a + $b; // 15
echo ($a > $b) ? "A is greater" : "B is greater";