array_key_exists()
$data = ['username' => 'Ashish', 'bio' => null];
// array_key_exists returns true because the key 'bio' is present
var_dump(array_key_exists('bio', $data)); // Output: bool(true)
var_dump()
$user = ["id" => 1, "active" => true];
var_dump($user);
/* Output:
array(2) {
["id"]=> int(1)
["active"]=> bool(true)
}
*/
array_push($stack, "Apple", "Raspberry");: Adds to the endarray_unshift(): Adds to the start$result = array_merge($defaults, $userSettings): merge the arraysFunction Declaration
function functionName() { }
$functionName = function() { };
function functionName ($arg1, $arg2) { }
echo: Does not have a return valueprint: Always returns the integer 1
echo "This ", "is ", "multiple ", "parameters."; // accepts multiple params
$result = print "Hello"; // $result is now
include: If the file is missing, it generates a Warning (E_WARNING). The script will continue to execute the remaining code.require: If the file is missing, it generates a Fatal Error (E_COMPILE_ERROR). The script will stop immediately.isset()
$a = "test";
if (isset($a)) {
echo "var set!";
}
$_SERVER: Contains information about headers, paths, and script locations.$_POST: Used to collect data submitted via an HTML form with themethod="post".$_COOKIE: Used to retrieve values from HTTP cookies.CSP (Content Security Policy) is an added layer of security used by web developers to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and Clickjacking.
Xdebug performs Server-side debugging and profiling in PHP.
The primary role of
fopen()is to open a file or a URL and return a file pointer resource (often called a “handle”). This resource is then used as a reference by other functions likefread(),fwrite(), andfclose()to perform specific operations on that file.Superglobals are always available in all scopes.
echo "Hello, " . $_GET['user'];error_log()is used for background logging. Default System Log: sends the message to the PHP error log defined in yourphp.inifile.file_get_contents()Reads everything into a string.debug_backtrace()function is a powerful tool used to generate a PHP backtrace. It provides a detailed “history” of the function calls that led to the exact point where the function was triggered.
function level3() {
print_r(debug_backtrace());
}
function level2() {
level3();
}
function level1() {
level2();
}
level1();
mkdir()is used to create directory in PHP.mysqli_real_escape_string()function creates a legal SQL string that can be used in an SQL statement. It takes a string and prepends backslashes to characters that could otherwise be misinterpreted by the MySQL database (such as single quotes, double quotes, and backslashes).- The
mysqli_connect()function is the primary way to open a new connection to a MySQL database server using the MySQL Improved (mysqli) extension in PHP.
$connection = mysqli_connect(host, username, password, dbname, port, socket);
- In PHP, a
try-catchstructure allows you to “try” a piece of code that might throw an error (an Exception). If an error occurs, the code inside thecatchblock is executed to handle it. - Visibility is strictly defined by these three keywords:
public: The property or method can be accessed from anywhere (inside the class, by child classes, and from external code).protected: The property or method can only be accessed within the class itself and by child classes (inheritance).private: The property or method can only be accessed within the specific class that defined it.
- Parameterized queries work by separating the SQL logic from the user data.
- Preparation: You send a query “template” to the database with placeholders (like
?or:name) instead of actual values. - Binding: You send the user data separately.
- Execution: The database engine treats the data strictly as values and never as part of the executable code. Even if a hacker inputs a malicious command (like
' OR 1=1), the database simply looks for a user whose name is literally that string.
- Preparation: You send a query “template” to the database with placeholders (like
- The
htmlentities()function takes a string and converts all applicable characters into HTML entities. For example, it turns a<into<and a>into> rmdir()function is used to remove directoryset_exception_handler()Sets a user-defined function to handle all uncaught exceptions.
function myGlobalExceptionHandler($exception) {
// This runs for any exception NOT inside a try-catch
error_log("Uncaught Exception: " . $exception->getMessage());
echo "Something went wrong. Our team has been notified.";
}
// Register the handler
set_exception_handler('myGlobalExceptionHandler');
// Throw an exception outside of a try-catch
throw new Exception("Critical Database Failure!");