You are on page 1of 3

Basic PHP Syntax

<?php
// PHP code goes here
?>
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
Variables Scope
local
global
static

<?php
$x = 5; // global scope
function myTest() {
$x = 5; // local scope
global $x;
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y']; //array called $GLOBALS[index]
static $x = 0;//Note: The variable is still local to the function.
myTest();

PHP echo and print Statements


echo or echo()
print or print()

PHP String Functions

strlen() - Return the Length of a String


echo strlen("Hello world!"); // outputs 12
The PHP str_word_count() function counts the number of words in a string.
echo str_word_count("Hello world!"); // outputs 2
The PHP strrev() function reverses a string.
echo strrev("Hello world!"); // outputs !dlrow olleH

Search for the text "world" in the string "Hello world!":

echo strpos("Hello world!", "world"); // outputs 6

Replace the text "world" with "Dolly":


echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!

Add a backslash in front of the character "W":


<?php
$str = addcslashes("Hello World!","W");
echo($str);
?>
Add a backslash in front of each double quote ("):
<?php
$str = addslashes('What does "yolo" mean?');
echo($str);
?>
The predefined characters are:

single quote (')


double quote (")
backslash (\)
NULL

https://www.w3schools.com/php/php_ref_string.asp

PHP min() and max() Functions


<?php
echo(min(0, 150, 30, 20, -8, -200)); // returns -200
echo(max(0, 150, 30, 20, -8, -200)); // returns 150
?>

The abs() function returns the absolute (positive) value of a number:


<?php
echo(abs(-6.7)); // returns 6.7
?>
The rand() function generates a random number:
<?php
echo(rand());
?>
<?php
echo(rand(10, 100));
?>
PHP Constant
define(name, value, case-insensitive)

Parameters:

name: Specifies the name of the constant


value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive.
Default is false

Constant Arrays

<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
?>

Constants are Global


This example uses a constant inside a function, even if it is defined outside the
function:

<?php
define("GREETING", "Welcome to W3Schools.com!");

function myTest() {
echo GREETING;
}

myTest();
?>

PHP Global Variables - Superglobals

The PHP superglobal variables are:


$GLOBALS
$_SERVER -headers, paths, and script locations
$_REQUEST used to collect data after submitting an HTML form
$_POST - collect form data after submitting an HTML form with method="post".
$_GET - collect form data after submitting an HTML form with method="get".
$_FILES
$_ENV
$_COOKIE
$_SESSION

You can easily do this by adding the following code in your wp-config.php file.

1
2
// Disallow file edit
define( 'DISALLOW_FILE_EDIT', true );

You can do this by opening a text editor like Notepad and paste this code:

1
2
3
<Files *.php>
deny from all
</Files>
Next, you need to save this file as .htaccess and upload it to /wp-content/uploads/
folders on your website using an FTP client.
https://www.wpbeginner.com/wordpress-security/

You might also like