PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
A delimiter is a sequence of one or more characters used to specify the boundary between separate regions in text or other data streams. In PHP, delimiters are often used to define the start and end of a sequence of characters for various functions.
Here are a few examples:
1. String Delimiters
In PHP, a string can be defined using single quotes (' ') or double quotes (" ").
$example1 = 'Hello, world!'; $example2 = "Hello, world!";
2. Regular Expression Delimiters
In PHP, when using regular expressions (with functions like preg_match()
, preg_replace()
, etc.), a delimiter must be used to indicate the start and end of the pattern.
$pattern = '/world/'; $subject = 'Hello, world!'; preg_match($pattern, $subject, $matches); print_r($matches);
Here, the forward slash /
is used as the delimiter for the regular expression world
.
3. Heredoc and Nowdoc Delimiters
PHP also supports two other types of string delimiters: Heredoc and Nowdoc. Heredoc and Nowdoc provide a way to declare large blocks of text without needing to escape every special character.
Heredoc:
$example = <<<EOD Hello, world! This is a text string. EOD;
In this example, EOD
is the Heredoc delimiter. It's important to note that the closing delimiter must appear on a line by itself and no indentation is allowed before or after the closing delimiter.
Nowdoc:
$example = <<<'EOD' Hello, world! This is a text string. EOD;
The Nowdoc syntax is very similar to the Heredoc syntax, but with the delimiter enclosed in single quotes. The difference is that the contents of a Nowdoc are treated as a single-quoted string, meaning that no variable substitution will occur.
Remember that the choice of delimiter you use depends on the context in which it's being used. Different situations call for different delimiters, and understanding how and when to use each one is an important part of mastering PHP.
PHP Delimiter Examples:
<?php // PHP code here ?>
Using Delimiters in PHP Code:
PHP code is enclosed between the <?php
and ?>
tags. For example:
<?php echo "Hello, World!"; ?>
PHP String Delimiters:
PHP supports both single ('
) and double ("
) quotes as string delimiters.
$singleQuoted = 'This is a single-quoted string.'; $doubleQuoted = "This is a double-quoted string.";
PHP Heredoc and Nowdoc Delimiters:
<<<
followed by a label and ends with the label on a line by itself.$heredoc = <<<EOD This is a heredoc string. It can span multiple lines. EOD;
$nowdoc = <<<'EOD' This is a nowdoc string. It also preserves formatting. EOD;
Custom Delimiters in PHP Functions:
Some PHP functions, like preg_match()
, allow you to use custom delimiters for regular expressions.
$pattern = '/^Hello/'; // is equivalent to $pattern = '#^Hello#';
Escaping Delimiters in PHP:
If you need to include the delimiter characters in a string, you can escape them using the backslash (\
) character.
$escapedString = "This is an example of escaping \$ and \?> inside a string.";
Delimiters in Regular Expressions in PHP: Regular expressions in PHP often use delimiters. You can choose different delimiters based on the pattern.
$pattern = '/^Start/'; // is equivalent to $pattern = '#^Start#';
Delimiter Usage in PHP for Templates: In templating engines, delimiters are often used to distinguish between PHP code and template content.
<html> <body> <h1><?= $pageTitle ?></h1> <p><?= $content ?></p> </body> </html>
PHP Delimiters for Different File Types (e.g., HTML, XML):
When embedding PHP in other file types, such as HTML or XML, you can use the common <?php
and ?>
delimiters.
PHP Delimiters and Security Considerations: Always be cautious about user input and avoid using user-provided data directly in PHP delimiters or evaluating code dynamically to prevent security vulnerabilities like code injection.