तो स्वागत है आपका। 👋
अगर आप Web Development सीखना चाहते हैं, तो PHP एक बहुत काम की Programming Language है। PHP की मदद से आप Dynamic Websites, Login Systems, Forms, Admin Panels और Database वाले Web Applications बना सकते हैं।
PHP की शुरुआत करने वाले Students को अक्सर बहुत सारे Functions, Variables, Arrays, Loops और Commands एक साथ देखने पड़ते हैं। इसी वजह से हमने यह Complete Cheat Sheet तैयार की है।
इसमें आपको PHP के 300+ जरूरी Functions, Concepts और Features आसान भाषा में मिलेंगे।
आप इसे Learning के साथ-साथ Quick Revision Guide की तरह भी इस्तेमाल कर सकते हैं।
💡 मेरा अनुभव
PHP सीखते समय सबसे पहले Variables और echo जैसे छोटे Concepts समझना आसान रहता है। इसके बाद धीरे-धीरे Conditions, Loops, Arrays और Functions सीखने चाहिए।
जब ये Basics अच्छे से समझ आ जाते हैं, तब Forms, Sessions, Cookies और MySQL जैसे Topics काफी आसान लगने लगते हैं।
इसलिए PHP सीखते समय एक साथ सब कुछ याद करने की कोशिश न करें। रोज़ थोड़ा Code लिखें और उसे खुद Run करके देखें।
PHP क्या है?
PHP एक Server-Side Programming Language है।
इसका इस्तेमाल मुख्य रूप से Websites और Web Applications बनाने के लिए किया जाता है।
PHP की मदद से आप:
Dynamic Web Pages बना सकते हैं।
Forms Handle कर सकते हैं।
Database से Data ले सकते हैं।
Login System बना सकते हैं।
Sessions और Cookies संभाल सकते हैं।
Files के साथ काम कर सकते हैं।
APIs के साथ काम कर सकते हैं।
एक Simple PHP Code:
<?php
echo "Hello World!";
?>Output:
Hello World!PHP Code कैसे लिखा जाता है?
PHP Code आमतौर पर:
<?php
// PHP Code
?>के अंदर लिखा जाता है।
उदाहरण:
<?php
$name = "Rahul";
echo $name;
?>PHP Comments
Single Line Comment:
// This is a commentया:
# This is a commentMultiple Line Comment:
/*
This is
a comment
*/PHP Variables
PHP में Variable बनाने के लिए $ Symbol का इस्तेमाल होता है।
$name = "Rahul";
$age = 20;
$city = "Delhi";Variable की Value देखने के लिए:
echo $name;PHP Variable के Rules
Variable:
$से शुरू होना चाहिए।Letter या
_से शुरू हो सकता है।Number से शुरू नहीं हो सकता।
इसमें Space नहीं होना चाहिए।
PHP में Variable Names Case-Sensitive होते हैं।
उदाहरण:
$name
$Name
$NAMEये तीनों अलग Variables हो सकते हैं।
PHP Data Types
PHP में कई तरह के Data Types होते हैं:
| Data Type | Example |
|---|---|
| String | "Hello" |
| Integer | 25 |
| Float | 10.5 |
| Boolean | true |
| Array | [1,2,3] |
| Object | Object |
| NULL | NULL |
| Resource | Resource |
String
$name = "Lovejeet";Single Quotes:
$name = 'Lovejeet';Double Quotes:
$name = "Lovejeet";Integer
$age = 25;Float
$price = 99.50;Boolean
$isLogin = true;या:
$isLogin = false;NULL
$value = null;Variable का Type देखें
var_dump($name);उदाहरण:
$name = "Rahul";
var_dump($name);Type Check Functions
is_string()
is_int()
is_float()
is_bool()
is_array()
is_object()
is_null()उदाहरण:
is_string($name);Constants
Constant की Value बाद में बदलने के लिए नहीं होती।
define("SITE_NAME", "CodeSardar");
echo SITE_NAME;Modern PHP में Constant के लिए:
const SITE_NAME = "CodeSardar";echo
Screen पर Data दिखाने के लिए:
echo "Hello";एक से ज्यादा चीज़ें:
echo "Hello ", "World";print "Hello World";String Concatenation
दो Strings को जोड़ने के लिए . इस्तेमाल होता है।
$first = "Hello";
$second = "World";
echo $first . " " . $second;Output:
Hello WorldString Functions
PHP में Strings के साथ काम करने के लिए बहुत सारे Functions हैं।
strlen()
String की Length:
$name = "CodeSardar";
echo strlen($name);strtoupper()
Text को Capital Letters में:
echo strtoupper("hello");strtolower()
Text को Small Letters में:
echo strtolower("HELLO");ucfirst()
पहले Letter को Capital करने के लिए:
echo ucfirst("hello");ucwords()
हर Word का पहला Letter Capital:
echo ucwords("hello world");trim()
String के आगे और पीछे की Extra Spaces हटाने के लिए:
$name = trim($name);str_replace()
Text बदलने के लिए:
$text = "Hello World";
echo str_replace("World", "PHP", $text);substr()
String का एक हिस्सा निकालने के लिए:
$text = "Hello World";
echo substr($text, 0, 5);strpos()
String में किसी Text की Position खोजने के लिए:
echo strpos("Hello World", "World");str_contains()
Check करें कि String में कोई Text मौजूद है या नहीं:
str_contains("Hello World", "World");explode()
String को Array में बदलने के लिए:
$data = "HTML,CSS,PHP";
$result = explode(",", $data);implode()
Array को String में बदलने के लिए:
$data = ["HTML", "CSS", "PHP"];
echo implode(",", $data);PHP Operators
PHP में Operators बहुत जरूरी हैं।
Arithmetic Operators
+
-
*
/
%
**उदाहरण:
$a = 10;
$b = 5;
echo $a + $b;Assignment Operators
=
+=
-=
*=
/=
%=उदाहरण:
$count = 10;
$count += 5;अब:
15Comparison Operators
==
===
!=
!==
>
<
>=
<=उदाहरण:
10 == "10"यह Type को ध्यान में नहीं रखता।
जबकि:
10 === "10"Value और Type दोनों Check करता है।
Logical Operators
&&
||
!उदाहरण:
if ($age >= 18 && $country == "India") {
echo "Allowed";
}if Statement
$age = 20;
if ($age >= 18) {
echo "Adult";
}if-else
$age = 16;
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}elseif
$marks = 75;
if ($marks >= 90) {
echo "A";
} elseif ($marks >= 60) {
echo "B";
} else {
echo "C";
}Ternary Operator
छोटे if-else के लिए:
$age = 20;
$result = ($age >= 18) ? "Adult" : "Minor";Null Coalescing Operator
$name = $_GET['name'] ?? "Guest";अगर name मौजूद नहीं है, तो "Guest" इस्तेमाल होगा।
switch
$day = 2;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
default:
echo "Unknown";
}PHP Loops
PHP में मुख्य Loops:
forwhiledo-whileforeach
for Loop
for ($i = 1; $i <= 5; $i++) {
echo $i;
}while Loop
$i = 1;
while ($i <= 5) {
echo $i;
$i++;
}do-while Loop
$i = 1;
do {
echo $i;
$i++;
} while ($i <= 5);foreach Loop
$names = ["Aman", "Ravi", "Rahul"];
foreach ($names as $name) {
echo $name;
}break
Loop रोकने के लिए:
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i;
}continue
Current Loop को Skip करने के लिए:
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i;
}PHP Arrays
Array में एक से ज्यादा Values रखी जा सकती हैं।
$colors = ["Red", "Green", "Blue"];Array Index
echo $colors[0];Output:
RedAssociative Array
$student = [
"name" => "Rahul",
"age" => 20,
"city" => "Delhi"
];Value:
echo $student["name"];Multidimensional Array
$students = [
[
"name" => "Rahul",
"age" => 20
],
[
"name" => "Aman",
"age" => 21
]
];Array Functions
count()
Array में कितने Items हैं:
count($colors);in_array()
Value मौजूद है या नहीं:
in_array("Red", $colors);array_push()
Array के अंत में Item जोड़ें:
array_push($colors, "Yellow");array_pop()
Last Item हटाएँ:
array_pop($colors);array_shift()
First Item हटाएँ:
array_shift($colors);array_unshift()
शुरुआत में Item जोड़ें:
array_unshift($colors, "Black");array_merge()
दो Arrays जोड़ें:
$result = array_merge($array1, $array2);sort()
Ascending Order:
sort($numbers);rsort()
Descending Order:
rsort($numbers);asort()
Associative Array को Value के अनुसार Sort करें:
asort($data);ksort()
Key के अनुसार Sort:
ksort($data);array_reverse()
Array को उल्टा करें:
$result = array_reverse($colors);array_unique()
Duplicate Values हटाएँ:
$result = array_unique($colors);array_slice()
Array का एक हिस्सा निकालें:
$result = array_slice($colors, 0, 2);PHP Functions
Function Code के एक काम को बार-बार इस्तेमाल करने में मदद करता है।
function greet() {
echo "Hello!";
}
greet();Function Parameters
function greet($name) {
echo "Hello " . $name;
}
greet("Rahul");Return Value
function add($a, $b) {
return $a + $b;
}
$result = add(10, 20);
echo $result;Default Parameter
function greet($name = "Guest") {
echo $name;
}Type Declaration
function add(int $a, int $b): int {
return $a + $b;
}Anonymous Function
$greet = function() {
echo "Hello";
};
$greet();Arrow Function
$add = fn($a, $b) => $a + $b;Useful Math Functions
PHP में Math के लिए कई Functions मिलते हैं।
abs()
ceil()
floor()
round()
max()
min()
pow()
sqrt()
rand()उदाहरण:
echo round(10.56);abs()
Negative Number को Positive Value में:
echo abs(-10);ceil()
ऊपर की तरफ Round:
echo ceil(10.2);floor()
नीचे की तरफ Round:
echo floor(10.8);sqrt()
Square Root:
echo sqrt(25);pow()
Power:
echo pow(2, 3);Date और Time
Current Date:
echo date("Y-m-d");Current Time:
echo date("H:i:s");Current Date और Time:
echo date("Y-m-d H:i:s");Date Formats
| Format | मतलब |
|---|---|
d | Day |
m | Month |
Y | Year |
H | Hour |
i | Minute |
s | Second |
l | पूरा Day Name |
F | पूरा Month Name |
time()
Current Unix Timestamp:
echo time();strtotime()
Date Text को Timestamp में बदलने के लिए:
echo strtotime("tomorrow");File Handling
PHP में Files के साथ भी काम किया जा सकता है।
मुख्य Functions:
fopen()
fclose()
fread()
fwrite()
file_get_contents()
file_put_contents()
file_exists()
filesize()
unlink()File पढ़ें
$content = file_get_contents("data.txt");
echo $content;File में लिखें
file_put_contents(
"data.txt",
"Hello PHP"
);File मौजूद है?
if (file_exists("data.txt")) {
echo "File exists";
}File Delete करें
unlink("data.txt");PHP Forms
HTML Form:
<form method="post">
<input type="text" name="username">
<button type="submit">Submit</button>
</form>PHP में:
$username = $_POST["username"] ?? "";GET और POST
| Method | इस्तेमाल |
|---|---|
| GET | URL के जरिए Data |
| POST | Request Body में Data |
GET Data
$name = $_GET["name"] ?? "";POST Data
$name = $_POST["name"] ?? "";Form Input को सुरक्षित दिखाना
User के दिए हुए Text को HTML में दिखाते समय:
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');यह HTML Special Characters को सुरक्षित तरीके से दिखाने में मदद करता है।
Sessions
Session का इस्तेमाल User की जानकारी को एक Page से दूसरे Page तक रखने के लिए किया जा सकता है।
Start:
session_start();Value:
$_SESSION["username"] = "Rahul";Read:
echo $_SESSION["username"];Session Delete:
unset($_SESSION["username"]);पूरा Session खत्म:
session_destroy();Cookies
Cookie बनाने के लिए:
setcookie(
"username",
"Rahul",
time() + 3600,
"/"
);Cookie पढ़ें:
echo $_COOKIE["username"] ?? "";Cookie Delete:
setcookie(
"username",
"",
time() - 3600,
"/"
);PHP Superglobals
PHP में कुछ Special Variables होते हैं जिन्हें Superglobals कहा जाता है।
| Superglobal | काम |
|---|---|
$_GET | GET Data |
$_POST | POST Data |
$_REQUEST | Request Data |
$_SERVER | Server Information |
$_SESSION | Session Data |
$_COOKIE | Cookie Data |
$_FILES | Uploaded Files |
$_ENV | Environment Data |
$GLOBALS | Global Variables |
$_SERVER
कुछ Server Information:
echo $_SERVER["REQUEST_METHOD"];Current Page:
echo $_SERVER["PHP_SELF"];File Upload
HTML:
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo">
<button type="submit">
Upload
</button>
</form>PHP में Uploaded File की जानकारी:
$_FILES["photo"];JSON
PHP में Array को JSON में:
$json = json_encode($data);JSON को PHP Data में:
$data = json_decode($json, true);MySQL के साथ PHP
PHP में MySQL Database से जुड़ने के लिए MySQLi या PDO का इस्तेमाल किया जा सकता है।
MySQLi Example:
$conn = new mysqli(
"localhost",
"root",
"",
"school"
);Connection Check:
if ($conn->connect_error) {
die("Connection failed");
}PDO Connection
$pdo = new PDO(
"mysql:host=localhost;dbname=school",
"root",
""
);Database Query
$result = $conn->query(
"SELECT * FROM students"
);Prepared Statements
User Input के साथ Database Query करते समय Prepared Statements का इस्तेमाल करना बेहतर होता है।
MySQLi:
$stmt = $conn->prepare(
"SELECT * FROM users WHERE email = ?"
);
$stmt->bind_param("s", $email);
$stmt->execute();PHP OOP
PHP में Object-Oriented Programming का भी इस्तेमाल किया जा सकता है।
मुख्य Concepts:
Class
Object
Property
Method
Constructor
Inheritance
Interface
Trait
Encapsulation
Class
class Student {
public $name;
}Object
$student = new Student();
$student->name = "Rahul";Method
class Student {
public function greet() {
echo "Hello";
}
}Constructor
class Student {
public function __construct() {
echo "Student Created";
}
}Inheritance
class Animal {
public function sound() {
echo "Sound";
}
}
class Dog extends Animal {
}Interface
interface Payment {
public function pay();
}Trait
trait Logger {
public function log() {
echo "Log";
}
}Access Modifiers
PHP में:
public
protected
privateका इस्तेमाल किया जाता है।
| Modifier | कहाँ Access |
|---|---|
| public | कहीं से |
| protected | Class और Child Class |
| private | उसी Class में |
Exception Handling
PHP में Errors को Handle करने के लिए:
try {
// Code
} catch (Exception $e) {
echo $e->getMessage();
}throw
throw new Exception("Something went wrong");isset()
Check करें कि Variable मौजूद है और null नहीं है:
if (isset($name)) {
echo $name;
}empty()
Check करें कि Value खाली मानी जा रही है या नहीं:
if (empty($name)) {
echo "Empty";
}unset()
Variable हटाने के लिए:
unset($name);is_numeric()
is_numeric("123");is_array()
is_array($data);print_r()
Array या Object को पढ़ने लायक तरीके से देखने के लिए:
print_r($data);var_dump()
Variable की Value और Type देखने के लिए:
var_dump($data);Namespace
बड़े PHP Projects में Namespace का इस्तेमाल किया जाता है।
namespace App\Models;require
require "header.php";require_once
एक ही File को एक बार Load करने के लिए:
require_once "config.php";include
include "header.php";include_once
include_once "header.php";PHP की Useful Functions List
नीचे कुछ जरूरी PHP Functions एक जगह देख सकते हैं:
echo
print
var_dump
print_r
isset
empty
unset
define
strlen
strtoupper
strtolower
ucfirst
ucwords
trim
ltrim
rtrim
str_replace
str_contains
strpos
strrpos
substr
explode
implode
sprintf
printf
count
in_array
array_push
array_pop
array_shift
array_unshift
array_merge
array_slice
array_unique
array_reverse
sort
rsort
asort
arsort
ksort
krsort
array_keys
array_values
array_map
array_filter
array_reduce
array_search
array_column
abs
ceil
floor
round
max
min
pow
sqrt
rand
date
time
strtotime
mktime
file_exists
file_get_contents
file_put_contents
fopen
fclose
fread
fwrite
unlink
json_encode
json_decode
htmlspecialchars
htmlentities
htmlspecialchars_decode
session_start
session_destroy
setcookie
password_hash
password_verify
headerPassword Hashing
Password को सीधे Database में Store करना सही तरीका नहीं है।
Hash बनाने के लिए:
$hash = password_hash(
$password,
PASSWORD_DEFAULT
);Password Check:
password_verify(
$password,
$hash
);Redirect
PHP में User को दूसरी Page पर भेजने के लिए:
header("Location: dashboard.php");
exit;PHP Security के जरूरी Tips
PHP Website बनाते समय Security का ध्यान रखना बहुत जरूरी है।
User Input को Validate करें।
Output में जरूरत के अनुसार
htmlspecialchars()इस्तेमाल करें।Database के लिए Prepared Statements इस्तेमाल करें।
Password को Hash करके Store करें।
Sensitive Information को Code में सीधे न लिखें।
Error Messages को Production Website पर जरूरत से ज्यादा न दिखाएँ।
File Upload में File Type और Size Check करें।
HTTPS इस्तेमाल करें।
💡 मेरी सलाह
अगर आप PHP Beginner हैं, तो Learning का यह Order रखें:
PHP Basics
↓
Variables
↓
Data Types
↓
Operators
↓
if-else
↓
Loops
↓
Arrays
↓
Functions
↓
Forms
↓
Sessions & Cookies
↓
MySQL
↓
OOP
↓
APIs
↓
Real Projectइस तरीके से सीखने पर आपको एक साथ बहुत सारी चीज़ों का दबाव महसूस नहीं होगा।
⚠️ Note
PHP का Version आपके Server या Hosting पर निर्भर कर सकता है। नई Website बनाते समय Supported PHP Version का इस्तेमाल करना बेहतर है। पुराने Tutorials में कुछ ऐसे Functions मिल सकते हैं जिन्हें नई PHP Versions में बदल दिया गया है या हटा दिया गया है।
PHP में Beginner कौन-से Projects बना सकता है?
Practice के लिए ये Projects अच्छे रहेंगे:
| Project | क्या सीखेंगे |
|---|---|
| Calculator | Variables, Operators |
| Login Page | Forms, Sessions |
| Contact Form | POST, Validation |
| To-Do List | CRUD |
| Student Management | MySQL |
| Blog | Database, PHP |
| URL Shortener | Forms, Database |
| Admin Panel | Sessions, MySQL |
| Notes App | CRUD |
| Simple E-commerce | Database, Sessions |
💡 Pro Tip
PHP सीखते समय हर Function को याद करने की जरूरत नहीं है।
आपको यह समझना ज्यादा जरूरी है कि किस काम के लिए कौन-सा Function इस्तेमाल करना है।
उदाहरण के लिए:
String की Length चाहिए →
strlen()Array की गिनती चाहिए →
count()JSON बनाना है →
json_encode()JSON पढ़ना है →
json_decode()Password Hash करना है →
password_hash()File पढ़नी है →
file_get_contents()
Practice करते-करते ये Functions अपने आप याद होने लगेंगे।
Quick PHP Cheat Sheet
| Topic | जरूरी चीज़ें |
|---|---|
| Output | echo, print |
| Variables | $name, $age |
| Types | String, Integer, Float, Boolean, Array |
| Conditions | if, else, elseif, switch |
| Loops | for, while, do-while, foreach |
| Arrays | Indexed, Associative, Multidimensional |
| Functions | function, return |
| Strings | strlen(), trim(), str_replace() |
| Files | fopen(), file_get_contents() |
| Forms | $_GET, $_POST |
| Sessions | $_SESSION |
| Cookies | setcookie() |
| Database | MySQLi, PDO |
| Security | Hashing, Validation, Prepared Statements |
| OOP | Class, Object, Inheritance |
| JSON | json_encode(), json_decode() |
Conclusion
PHP एक ऐसी Programming Language है जिससे आप छोटे Web Projects से लेकर बड़े Dynamic Web Applications तक बना सकते हैं।
इस Complete Cheat Sheet में हमने PHP के जरूरी Functions, Variables, Operators, Conditions, Loops, Arrays, Functions, Forms, Sessions, Cookies, Files, MySQL, OOP, JSON और Security Concepts को आसान भाषा में देखा।
अगर आप अभी PHP सीखना शुरू कर रहे हैं, तो पहले Basics मजबूत करें और फिर धीरे-धीरे Database और OOP की तरफ जाएँ।
सबसे अच्छा तरीका यही है कि हर Topic के बाद खुद एक छोटा Program बनाएँ।
Code लिखिए → Error देखिए → गलती समझिए → फिर दोबारा Run कीजिए।
इसी तरह Practice करते-करते PHP काफी आसान लगने लगेगी। 🚀
FAQs
1. PHP क्या है?
PHP एक Server-Side Programming Language है जिसका इस्तेमाल Dynamic Websites और Web Applications बनाने के लिए किया जाता है।
2. PHP सीखने के लिए पहले क्या आना चाहिए?
HTML की Basic जानकारी होना अच्छा है। इसके बाद आप PHP सीखना शुरू कर सकते हैं।
3. PHP में Variable कैसे बनाते हैं?
PHP में Variable $ Symbol से शुरू होता है।
$name = "Rahul";4. PHP में echo का क्या काम है?
echo का इस्तेमाल Browser पर Text या Value दिखाने के लिए किया जाता है।
5. PHP में Array क्या होता है?
Array में एक से ज्यादा Values को एक Variable में रखा जा सकता है।
$names = ["Rahul", "Aman", "Ravi"];6. PHP में $_GET और $_POST क्या हैं?
ये PHP के Superglobals हैं। $_GET URL के जरिए आने वाले Data को पढ़ने में मदद करता है और $_POST Request Body से Data लेने के लिए इस्तेमाल होता है।
7. PHP को MySQL के साथ इस्तेमाल कर सकते हैं?
हाँ। PHP में MySQL Database से जुड़ने के लिए MySQLi और PDO जैसे तरीके इस्तेमाल किए जा सकते हैं।
8. PHP में Password कैसे सुरक्षित रखें?
Password को सीधे Store करने के बजाय password_hash() से Hash करके Store करें और Login के समय password_verify() से Check करें।
9. PHP में Session क्या है?
Session की मदद से User से जुड़ी जानकारी को अलग-अलग Pages के बीच रखा जा सकता है, जैसे Login User की जानकारी।
10. PHP सीखने का सबसे अच्छा तरीका क्या है?
PHP के छोटे-छोटे Programs बनाकर Practice करें। Calculator, Login Page, Contact Form और Student Management जैसे Projects से शुरुआत करना आसान रहेगा।
🎬 Need help? Click the button below to watch the complete step-by-step video guide tutorial!
📺 Watch Full Video Guide