- Sessions: Store data on the server, unique for each user.
- Cookies: Store data on the client/browser.
Category: PHP
-
sessions and cookies in PHP?
-
include and require?
include
→ gives a warning if file missing, script continues.require
→ gives a fatal error and stops execution.
-
superglobals in PHP?
Predefined global arrays available anywhere in PHP:
$_GET
– data from URL query string$_POST
– data from forms$_SESSION
– session variables$_COOKIE
– cookies$_SERVER
– server/environment info$_FILES
– file uploads$_REQUEST
– combines GET + POST + COOKIE
-
== and === in PHP?
==
checks for value equality (ignores type).===
checks for value and type equality.
var_dump(5 == "5"); // true var_dump(5 === "5"); // false
-
What are PHP data types?
- String
- Integer
- Float (double)
- Boolean
- Array
- Object
- NULL
-
echo and print in PHP?
echo
can output multiple strings, and is slightly faster.print
can only output one string, and always returns1
.
echo "Hello ", "World"; // works print "Hello World"; // works
-
What is PHP and why is it used?
- PHP stands for Hypertext Preprocessor.
- It is a server-side scripting language used to create dynamic web applications.
- It can connect with databases, handle forms, manage sessions/cookies, etc.
-
File Handling
<?php $file = fopen("example.txt", "w"); fwrite($file, "Hello File Handling!"); fclose($file); $file = fopen("example.txt", "r"); echo fread($file, filesize("example.txt")); fclose($file); ?>
Explanation:
"w"
→ write mode (creates/overwrites file)."r"
→ read mode.fwrite
writes text into file,fread
reads it.
-
String Functions
<?php $text = " Hello PHP! "; echo strlen($text); // 11 echo strtolower($text); // hello php! echo strtoupper($text); // HELLO PHP! echo trim($text); // removes extra spaces echo str_replace("PHP", "World", $text); // Hello World! ?>
Explanation:
PHP has many string functions for modifying and formatting text. -
Switch Case Example
<?php $day = "Monday"; switch ($day) {
} ?>case "Monday": echo "Start of the week!"; break; case "Friday": echo "Weekend is near!"; break; default: echo "Just another day.";
Explanation:
switch
is an alternative to multipleif-else
statements.- If
$day = "Monday"
, output will be Start of the week!