Sending Emails

Sessions and Cookie

Object Oriented

PHP and MYSQL

File Handling

Date and Time

Error Handling

Regular Expressions

0 evangelinestamb December 12, 2020

A regular expression is the sequence of characters that helps us to find a particular word or sentence from the sentence or paragraph. Regular expressions may be of a single word or of a complicated one.

We can use regular express to find or replace particular word or sentence from the sentence or paragraph.

Syntax

In PHP, Regular expression contains delimiters, a pattern and optional modifiers as shown below.

$website = "/codehelppro/i";

In the above example, / is the delimiters and codehelppro is the search patterns and i is the modifier which is optional.

Regular Expression Functions

In PHP, we have many in-built functions that helps us to match the patterns or regular expressions.

FunctionUse
preg_match()Match Reguar Expression
preg_match_all()Match global Regular Expression
preg_replace()Find and Replace using Regular Expression
preg_grep()Return array that match the regular expression
preg_split()Split that match the regular expression
preg_quote()Quote matched character from string

Note: preg_match() function stop searching after finding first match but preg_match_all() result all the match output.

1. preg_match() function

preg_match() function will check whether the given regular expression exist on string or not. Let’s look one example to make clear concepts.

<?php

$string = "Welcome to Google";
echo preg_match("/google/i", $string);
//Output : 1

?>

2. preg_match_all() Function

preg_match_all() function will result all the match number using regular expression. Let’s look one example to make clear concepts.

<?php

$str = "Welcome to Code Help Pro to learn Programing.";
$pattern = "/pro/i";
echo preg_match_all($pattern, $str); 
// Output : 2

?>

3. preg_replace() Function

preg_replace() function replaces the match string using a regular expression. Let’s look at one example to make clear concepts.

<?php

$string = "Visit Google!";
$pattern = "/google/i";
echo preg_replace($pattern, "Code Help Pro", $string); 
// Output : "Visit Code Help Pro!"

?>

4. preg_grep() Function

preg_grep() function return array entries that match given patten or regular expression. Let’s look at one example to make clear concepts.

<?php

$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);

?>

The above program return all the array elements conatining floating point numbers.

5. preg_split() Function

preg_split() function split the string using pattern or regular expressions. Let’s look at one example to make clear concepts.

<?php

$keywords = preg_split("/[\s,]+/", "Thanks, Code Help Pro");
print_r($keywords);

?>

The above program split the string by spaces or commas or line break . The output of the above program is:

Array
(
    [0] => Thanks
    [1] => Code
    [2] => Help
    [3] => Pro
)

6. preg_quote() Function

preg_quote() function quotes the match pattern or regular expression. Let’s look at one example to make clear concepts.

<?php

$string = 'Welcome to Code / Help / Pro';
$string = preg_quote($string, '/');
echo $string; 

?>

Regular Expression Modifiers

Regular Expression search depends on the modifiers.

ModifierDescription
iReturn output after the case-insensitive search.
mPerform multiple searches on the string.
uThis will enable the correct matching of UTF-8 encoded regular expression.

Regular Expression Pattern

ExpressionDescription
[abc]Find the one character from the option inside the brackets
[^abc]Detect any character, not inside the brackets
[0-9]Get one number range from 0 to 9

Predefined Variables

0 bettenanson4178 December 12, 2020

PHP provides a large number of predefined variables to all the code or scripts or projects. PHP also provides a predefined array containing variables that depend upon the server. Here, we will discuss most of the predefined variables.

All the variables we will discuss below are easily available with every scope.

PHP Superglobal Variables

Superglobal are those variables which are access in every where with any scope.

$GLOBALS Variables

An array containing all the variables that is can be accessed globally inside the script. The key of the array is the variable name.

<?php 

function website(){
    echo "Welcome to : ".$GLOBALS["website"];
   echo "Welcome to : ".$website;
}

?>

The output of the above program is:

Welcome to : Example Content
Welcome to : Example Content

Note: There is no need to declare the global $variable; to access inside the functions or methods. It can be access any where at nay time.

$SERVER Variables

This is the array containing information such as headers, scripts location, and path. This array is created by the server so this array may be different in different servers.

There are some keys that you can access using $SERVER variables.

SNKey and Description
1PHP_SELF
This is the key result of the currently executing script relative to the root document. For Example, if the $SERVER[‘PHP_SELF’] variable runs in https://codehelppro.com/detail/php/predefined-variables.php then the result would be /detail/php/predefined-variables.php
2SERVER_ADDR
Results IP address of where script currently running.
3SERVER_HOST
This will return the server name of where the script currently running. It is usually a domain or sub-domain name.
4REQUEST_METHOD
Return the request method to access the page. eg: GET, POST, etc.
5REQUEST_TIME
This is the key that will result in the timestamp of the start of the request.
6DOCUMENT_ROOT
Results the root document path under which the script is running.
7HTTP_ACCEPT
If there is a current request then it returns the contents of the request: header.
8HTTP_ACCEPT_CHARSERT
If there is a current request then it returns the contents of the Accept-Charset: header.
9HTTP_ACCEPT_ENCODING
If there is a current request then it returns the contents of the Accept-Encoding: header. Eg: ‘gzip’
10HTTP_ACCEPT_LANGUAGE
If there is a current request then it returns the contents of the Accept-Language: header. Eg: ‘en’
11HTTP_CONNECTION
If there is a current request then it returns the contents of the Connection: header. Eg: ‘Keep Alive’
12HTTP_HOST
If there is a current request then it returns the contents of the Host: header.
13HTTP_REFERER
This will return the referred user agent to the current page.
14HTTP_USER_AGENT
If there is a current request then it returns the contents of the User-Agent: header.
15HTTPS
If you are requesting through an HTTPS connection, then it will return a non-empty string.
16SCRIPT_ADMIN
This will returns the current script’s path.
17REQUEST_URI
This returns a URL to access this page. For example index.html
18SERVER_PORT
This will return the port of the currently running script.
$SERVER Variables Key

$GET Variables

This is the collection of array that is passed through the URL. We will learn this variable in details in next chapter.

$POST Variables

This is the collection of the array that is passed through the HTTP POST Request. We will learn this variable in detail in the next chapter.

$FILES Variables

This is the HTTP file upload variables. It is the collection of the array that is passed through the HTTP POST request for uploading files.

Let’s assume you have the html form as given below.

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="image" />
    <input type="submit" value="Upload Image" />
</form>

If we submit a post request after uploading the file then we will get the following sample array. To get that array, We have to print the $_FILES variables.

Array
(
      [name] => MyFile.txt (comes from the browser, your uploaded file name)
      [type] => text/plain
      [tmp_name] => /tmp/php/php1h4j1o (from your system)
      [error] => UPLOAD_ERR_OK  (= 0)
      [size] => 123   (the size in bytes)
)

You have to use the above array to upload the file on your server.

$REQUEST Variable

This is the collection of the array that contents the variables of $_GET, $_POST, and $_COOKIE variables. You can access all of these variable’s values using a $_REQUEST variable.