Recently discussions PHP language on habré come down more to the ability to design complex systems, which is good news. However, after reviewing a dozen of the most recognized web frameworks (Zend Framework, Adept, CakePHP, CodeIgniter, LIMB, Symfony, MZZ and others), I was sincerely surprised to find significant shortcomings in some of them in terms of elementary optimization.

In order for this topic to be more technically oriented, the results are presented in a more strict form, which may complicate the perception somewhat.

So, let's go ... The task is extremely simple: to conduct experiments on the speed of forming strings from substrings in single and double quotes. In principle, this question will still be relevant for a long time due to the peculiarities of string processing in PHP.

There are many articles on basic script optimization both in Russian and in other languages. Little is said about strings, but the fact of "parsing" strings in double quotes for variables and control characters is noted (however, as in the official documentation). Based on this, it is logical to assume that using double-quoted strings at work will be somewhat slower than the same operations with single-quoted substrings.

In addition to substituting variables into strings and concatenating variables with substrings, PHP implements at least one more way to form strings: using the sprintf function. It is logical to assume that this method will be significantly inferior to the "standard" ones due to an extra function call and string parsing inside.

The only addition before I present you the test script code: you need to consider 2 possible options working with strings in double quotes: considering the simple and "advanced" coding style. The fact that the variables are at the very beginning of the lines is probably not worth paying attention to - they are only examples:
$string = "$_SERVER["HTTP_HOST"] is not the administration of the Ulyanovsk region. We love the Russian language and don't like those who speak it..."
and
$string = "($_SERVER["HTTP_HOST"]) is not the administration of the Ulyanovsk region. We love the Russian language and don't like those who speak it..."

Test number one.
Well, it seems that all the reservations have been made - it's time to show the results of the work. Source tester can be found.

The screenshots show that my hypothesis was not confirmed. The assumption about working with strings through sprintf turned out to be the only correct one. The fastest were the functions that work with double quotes.

After a short reflection on the situation, the explanation came by itself: the whole point is that the reference string into which the substitutions were made is too short: the parser's passage through such a string is a trifling matter. However, even here it is clear that native substitution of a variable into a string gives an advantage over the "advanced style".
This is also the weakness of the concatenation approach: the volume of inserted data exceeds the volume of substrings. Where overheads come from can be read in the already mentioned habratopic.

However, even these thoughts needed to be confirmed. For this, a second test was needed with changes to the possible reasons mentioned for such unpredictable (for me) behavior. Apparently, a lot of things have been tweaked in the fifth version (I confess that in the fifth version of php I only conducted 1 test: to bypass array elements).

Test number two.
The second hypothesis is that lengthening the reference string will eventually increase the percentage of tester functions associated with the formation of strings in double quotes, relative to the results of test number 1. The same situation, theoretically, should be observed with respect to the operation of the sprintf function. This is due, first of all, to the need for string parsing and the increase in time spent on it. In the situation with the concatenation of substrings in single quotes, I think that the result will be approximately the same as in the first test, which will give a slight decrease in the proportion of the execution time of the quotes_3() function by the time of the entire script (but not a performance increase).

Conclusions, in fact, only positive and confirming the hypothesis. When the reference string is slightly increased, there is a large load, which leads to a drop in performance of the double quotes and sprintf functions.

The assumption about strings in single quotes also turned out to be correct: instead of 36.75% of the time in the first test, in the second, the quotes_3() function took 33.76% of the script execution time

practical value.
In simple terms, abstracting from the data, we can conclude: what longer string, in which it is necessary to perform substitution, the more likely that the concatenation operation will be completed faster than searching for a variable in double quotes. Volunteers can try to choose the necessary insertion parameters (number of variables, length of the reference string, lengths of strings in the variables) such that they satisfy the equality of execution times.

That, in fact, is all. It remains only to add that there are no trifles in programming (this is for those who like to say “saving on matches” (c) Adelf). Knowing such subtleties and taking them into account, you can write code that will be optimized at all its levels;)

PS:
Tests performed using Zend Studio For Eclipse 6.0.0 (Debugger + Profiler included).
PHP Version 5.2.5
Debian Linux OS

PPS:
I would be glad if someone will post their results of these tests. I think this will allow a more objective assessment of the need to use one or another method of substitution into strings. I would also be grateful for healthy criticism of the style of presentation and design.

(PHP 4, PHP 5, PHP 7)

str_replace- Replaces all occurrences of the search string with the replacement string

Description

This function returns a string or array with all occurrences of search in subject replaced by replace .

If you don't need complex find/replace rules (such as regular expressions), this function is preferable. preg_replace().

Parameter List

If search and replace are arrays, then str_replace() uses each value from the corresponding array to search and replace in subject . If there are fewer elements in the replace array than in search , the empty string will be used as the replacement string for the remaining values. If search is an array and replace is a string, then that replacement string will be used for each element of the search array. The reverse case doesn't make sense.

If search or replace are arrays, their elements will be processed from first to last.

The value you are looking for, also known as needle(needle). You can use an array for multiple search values.

Replace

The replacement value will be used to replace the searched search values. You can use an array for multiple values.

subject

The string or array to be searched and replaced, also known as haystack(a stack of hay).

If subject is an array, then the search and replace will be performed on each element of subject , and the result of the function will also be an array.

If passed, it will be set to the number of replacements made.

Return Values

This function returns a string or array with the values ​​replaced.

Examples

Example #1 Usage examples str_replace()

// assigns
$bodytag = str_replace("%body%" , "black" , " " );

// assign: Hll Wrld f PHP
$vowels = array("a" , "e" , "i" , "o" , "u" , "A" , "E" , "I" , "O" , "U" );
$onlyconsonants = str_replace ($vowels , "" , "Hello World of PHP" );

// assigns: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits" , "vegetables" , "fiber" );
$yummy = array("pizza" , "beer" , "ice cream" );

$newphrase = str_replace ($healthy , $yummy , $phrase );

// assign: 2
$str = str_replace ("ll" , "" , "good golly miss molly!" , $count );
echo $count ;
?>

Example #2 Examples of potential tricks with str_replace()

// Replacement Order
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order = array("\r\n" , "\n" , "\r" );
$replace = "
" ;

// Handles \r\n first to avoid replacing them again.
echo $newstr = str_replace ($order , $replace , $str );

// Outputs F, because A is replaced by B, then B by C, and so on...
// As a result, E will be replaced by F, since the replacement occurs from left to right.
$search = array("A" , "B" , "C" , "D" , "E" );
$replace = array("B" , "C" , "D" , "E" , "F" );
$subject = "A" ;
echo str_replace ($search , $replace , $subject );

// Outputs: apple, nut, nut (for the reason above)
$letters = array("I" , "o" );
$fruit = array("apple" , "nut" );
$text = "I'm about" ;
$output = str_replace ($letters , $fruit , $text );
echo $output ;
?>

Notes

Comment: This function is safe to handle data in binary form.

Warning

Note on replacement order

Because str_replace() replaces from left to right, then when using multiple replacements, it can replace a previously inserted value with another. See also examples on this page.

Comment:

This function is case sensitive. Use str_place() for a case-insensitive replacement.

Comment: In PHP 7.0.0 on 64-bit platforms there is no achievable string length limit, on 32-bit systems and earlier versions of PHP, strings cannot be larger than 2 GB (2147483647 bytes).

Syntax

A string can be defined with four different ways:

  • single quotes
  • double quotes
  • nowdoc syntax (since PHP 5.3.0)

Single quotes

The simplest way to define a string is to enclose it in single quotes (symbol " ).

To use a single quote within a string, escape it with a backslash ( \ ). If you need to write the backslash itself, duplicate it ( \\ ). All other backslashes will be interpreted as regular characters, which means that if you try to use other escape sequences such as \r or \n, they will be output as is instead of any special behavior.

echo "this is a simple string";

echo "Also, you can insert into lines
newline character like this,
this is normal"
;

// Outputs: One day Arnold said: "I"ll be back"
echo "One day Arnold said, "I\"ll be back"";

echo "Did you delete C:\\*.*?";

// Outputs: Did you delete C:\*.*?
echo "Did you delete C:\*.*?" ;

// Output: This will not be expanded: \n new line
echo "This will not expand: \n newline";

// Prints: $expand variables are also not expanded by $either
echo "Variables $expand also $either are not expanded";
?>

Double quotes

If the string is enclosed in double quotes ("), PHP recognizes the following special character escape sequences:

Escape sequences
Subsequence Meaning
\n newline (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\e escape character (ESC or 0x1B(27) in ASCII) (since PHP 5.4.4)
\f page feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\" double quote
\{1,3} sequence of characters matching a regular expression of a character in octal that silently overflows to fit in a byte (i.e. "\400" === "\000")
\x(1,2) a sequence of characters that matches a character regular expression in hexadecimal
\u(+) sequence of characters matching a Unicode character regular expression that maps to a string in UTF-8 representation (added in PHP 7.0.0)

As with a string enclosed in single quotes, escaping any character will also print the escape character itself. Prior to PHP 5.1.1, the backslash in \($var) not printed.

Heredoc

The third way to define strings is using the heredoc syntax: <<< . After this statement, you must specify an identifier, then a newline. After that comes the line itself, and then the same identifier that closes the insert.

Line must start with a closing identifier, i.e. it should be in the first column of the row. In addition, the identifier must follow the same naming rules as all other PHP tags: contain only alphanumeric characters and underscores, and must not start with a number (underscores are allowed).

Attention

It is very important to note that the string with the closing identifier must not contain other characters, except for the semicolon ( ; ). This means that the identifier must not be indented and that there can't be any spaces or tabs before or after the semicolon. It is also important to understand that the first character before the closing identifier must be a newline character, as defined by your operating system. For example, on UNIX systems, including macOS, this is \n. The closing identifier must also be immediately followed by a new line.

If this rule is violated and the closing identifier is not "clean", it is considered that there is no closing identifier and PHP will continue searching for it. If, in this case, the correct closing identifier is never found, then this will cause a parsing error with a line number at the end of the script.

Beispiel #1 Incorrect syntax example

class foo(
public $bar =<<bar
EOT;
// padding before the closing identifier is not allowed
}
?>

Beispiel #2 Correct syntax example

class foo(
public $bar =<<bar
EOT;
}
?>

Heredoc cannot be used to initialize class fields. As of PHP 5.3, this restriction only applies to heredocs that contain variables within them.

Heredoc text behaves the same as a double-quoted string without having them. This means you don't need to escape quotes in the heredoc, but you can still use the above escape sequences. Variables are handled, but you need to be as careful when using complex variables inside a heredoc as you would when working with strings.

Beispiel #3 heredoc string definition example

$str =<<Line example,
spanning multiple lines
using the heredoc syntax.
EOD;

Class foo
{
var $foo ;
var $bar ;

Function __construct()
{
$this -> foo = "foo" ;
$this ->
}
}

$foo = newfoo();
$name = "Name" ;

echo<<My name is " $name ". I type $foo -> foo .
Now I'm taking out
( $foo -> bar [ 1 ]) .
This should output an uppercase "A": \x41
EOT;
?>

My name is "Name". I am typing Foo. Now, I am outputting Bar2. This should output an uppercase "A": A

It is also possible to use heredoc syntax to pass data through function arguments:

Since version 5.3.0, it has become possible to initialize static variables and class properties/constants using the heredoc syntax:

Beispiel #5 Using heredoc to initialize static variables

// Static variables
functionfoo()
{
static $bar =<<There's nothing here...
label;
}

// Class constants/properties
class foo
{
const BAR =<<An example of using a constant
FOOBAR;

Public $base =<<Field usage example
FOOBAR;
}
?>

As of PHP 5.3.0, it is also possible to surround the Heredoc identifier with double quotes:

Nowdoc

Nowdoc is the same for single-quoted strings as heredoc is for double-quoted strings. Nowdoc is similar to heredoc, but inside it no substitutions are made. This construct is ideal for embedding PHP code or other large blocks of text without having to escape it. In this it is a bit similar to the SGML construct. by declaring a block of text not intended to be processed.

Nowdoc is indicated by the same sequence <<< , which is used in the heredoc, but the identifier that follows it is enclosed in single quotes, for example, <<<"EOT" . All conditions that apply to heredoc identifiers also apply to nowdoc, especially those that apply to the closing identifier.

Beispiel #7 nowdoc example

echo<<<"EOD"
text example,
spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
for example, \\ and \".
EOD;

The result of running this example:

An example of text spanning multiple lines using the nowdoc syntax. Backslashes are always treated literally, like \\ and \".

Example #8 Nowdoc string quoting example with variables

/* More complex example with variables. */
class foo
{
public $foo ;
public $bar ;

Function __construct()
{
$this -> foo = "foo" ;
$this -> bar = array("Bar1" , "Bar2" , "Bar3" );
}
}

$foo = newfoo();
$name = "Name" ;

echo<<<"EOT"
My name is "$name". I am typing $foo->foo.
Now I'm typing ($foo->bar).
This should not output a capital "A": \x41
EOT;
?>

The result of running this example:

My name is "$name". I am typing $foo->foo. Now I'm typing ($foo->bar). This should not output a capital "A": \x41

Example #9 Static data example

class foo(
public $bar =<<<"EOT"
bar
EOT;
}
?>

Comment:

nowdoc support was added in PHP 5.3.0.

Processing Variables

If a string is specified in double quotes or with a heredoc, the variables inside it are processed.

There are two types of syntax: simple and complex. The simple syntax is easier and more convenient. It makes it possible to process a variable, an array value ( array) or object properties ( object) with minimal effort.

Complex syntax can be identified by the curly braces surrounding the expression.

Simple Syntax

If the interpreter encounters a dollar sign ( $ ), it captures as many characters as possible to form the correct variable name. If you want to specify the end of a name, enclose the variable name in curly braces.

$juice = "apple" ;

echo "He drank some $juice juice." . PHP_EOL ;

// Incorrect. "s" is a valid character for a variable name, but the variable name is $juice.
echo "He drank some juice made of $juices ." ;

// Correct. The end of the variable name is strictly specified using brackets:
echo "He drank some juice made of $( juice ) s." ;
?>

The result of running this example:

He drank some apple juice. He drank some juice made of. He drank some juice made of apples.

An element of an array ( array) or object property ( object). In array indices, the closing square bracket ( ] ) marks the end of the index definition. The same rules apply to object properties as to simple variables.

Example #10 Simple syntax example

define("KOOLAID" , "koolaid1" );
$juices = array("apple" , "orange" , "koolaid1" => "purple" );

echo "He drank some $juices [ 0 ] juice." . PHP_EOL ;
echo "He drank some $juices [ 1 ] juice." . PHP_EOL ;
echo "He drank some $juices [ koolaid1 ] juice." . PHP_EOL ;

class people(
public $john = "John Smith" ;
public $jane = "Jane Smith" ;
public $robert = "Robert Paulsen" ;

Public $smith = "Smith" ;
}

$people = new people();

echo " $people -> john drank some $juices [ 0 ] juice." . PHP_EOL ;
echo " $people -> john then said hello to $people -> jane ." . PHP_EOL ;
echo " $people -> john "s wife greeted $people -> robert." . PHP_EOL ;
echo " $people -> robert greeted the two $people -> smiths ." ; // Won't work
?>

The result of running this example:

He drank some apple juice. He drank some orange juice. He drank some purple juice. John Smith drank some apple juice. John Smith then said hello to Jane Smith. John Smith's wife greeted Robert Paulsen. Robert Paulsen greeted the two.

PHP 7.1.0 added support negative numeric indexes.

Example #11 Negative numeric indices

$string = "string" ;
echo "The character at index -2 is$string [- 2 ] ." , PHP_EOL ;
$string [- 3 ] = "o" ;
echo "Changing the character at position -3 to 'o' produces the following line:$string ." , PHP_EOL ;
?>

The result of running this example:

The character with index -2 is n. Changing the character at position -3 to "o" produces the following string: strong

For anything more complex, use complex syntax.

Complex (curly) syntax

It is called complex not because it is difficult to understand, but because it allows the use of complex expressions.

Any scalar variable, array element, or object property mapped to a string can be represented in a string with this syntax. Just write the expression just like you would outside the string and then enclose it in { and } . Because the { cannot be escaped, this syntax will only be recognized when $ follows directly after { . Use {\$ to print {$ . A few illustrative examples:

// Show all errors
error_reporting(E_ALL);

$great = "great" ;

// Doesn't work, outputs: This (great)
echo "This is ( $great )" ;

// Works, outputs: This is great
echo "This is ($great)" ;

// Works
echo "This square is wide( $square -> width ) 00 centimeters." ;

// Works, quoted keys only work with curly bracket syntax
echo "This works: ( $arr [ "key" ]) " ;

// Works
echo "This works: ( $arr [ 4 ][ 3 ]) " ;

// This is wrong for the same reason that $foo is outside
// lines. In other words, it will still work
// but since PHP looks for the foo constant first, this will call
// E_NOTICE level error (undefined constant).
echo "It is not right:( $arr [ foo ][ 3 ]) " ;

// Works. When using multidimensional arrays inside
// strings always use curly braces
echo "This works: ( $arr [ "foo" ][ 3 ]) " ;

// Works.
echo "This works: " . $arr [ "foo" ][ 3 ];

echo "This also works:( $obj -> values ​​[ 3 ]-> name ) " ;

echo "This is the value of the variable named$name : ($( $name )) " ;

echo "This is the value of the variable by name, which is returned by the getName() function:($( getName ())) ";

echo "This is the value of the variable by name returned by \$object->getName():($( $object -> getName ())) " ;

// Doesn't work, outputs: This is what getName() returns: (getName())
echo "This is what getName() returns: (getName())";
?>

It is also possible to access object properties within strings using this syntax.

class foo(
var $bar = "I am bar." ;
}

$foo = newfoo();
$bar = "bar" ;
$baz = array("foo" , "bar" , "baz" , "quux" );
echo " ( $foo -> $bar ) \n" ;
echo " ( $foo ->( $baz [ 1 ])) \n" ;
?>

The result of running this example:

I am bar. I am bar.

Comment:

Functions, method calls, static class variables, and class constants work internally {$} , since PHP version 5. However, the specified value will be treated as a variable name in the same context as the string in which it is defined. Using single curly braces ( {} ) will not work for accessing the values ​​of functions, methods, class constants, or static class variables.

// Show all errors
error_reporting(E_ALL);

class beers(
const softdrink = "rootbeer" ;
public static $ale = "ipa" ;
}

$rootbeer = "A & W" ;
$ipa = "Alexander Keith\"s" ;

// This works, outputs: I would like A & W
echo "I'd like ($( beers :: softdrink )) \n" ;

// This also works, outputs: I would like Alexander Keith"s
echo "I'd like ($( beers :: $ale )) \n" ;
?>

Accessing a character in a string and changing it

Characters in strings can be used and modified by specifying their offset from the beginning of the string, starting at zero, in square brackets after the string, for example, $str . Think of a string for this purpose as an array of characters. If you need to get or replace more than 1 character, you can use the functions substr() and substr_replace().

Comment: since PHP 7.1.0, negative offset values ​​are supported. They set the offset from the end of the string. Previously, negative offsets caused a level error E_NOTICE when read (returning an empty string) or E_WARNING on write (leaving the string unchanged).

Comment: A character in a string can also be accessed using curly braces, such as $str(42) .

Attention

Attempting to write to an offset outside the string will pad the string with spaces up to that offset. Non-integer types will be converted to integers. Wrong offset type will cause level error E_WARNING. Only the first character of the assigned string is used. As of PHP 7.1.0, assigning an empty string will cause a fatal error. Previously, in this case, a null byte (NULL) was assigned.

Attention

Strings in PHP are internally arrays of bytes. As a result, accessing or modifying a string by offset is not multibyte safe, and should only be done with strings in single-byte encodings such as ISO-8859-1.

Comment: Since PHP 7.1.0, the use of an empty index causes a fatal error, previously in such a case the string was converted to an array without warning.

Example #12 Several example strings

// Get the first character of the string
$str = "This is a test." ;
$first = $str [ 0 ];

// Get the third character of the string
$third = $str [ 2 ];

// Get the last character of the string
$str = "This is still a test." ;
$last = $str [ strlen($str )- 1 ];

// Change the last character of the string
$str = "Look at the sea" ;
$str [ strlen ($str )- 1 ] = "e" ;

?>

As of PHP 5.4, the offset in a string must be either an integer or a string containing numbers, otherwise a warning will be issued. Previously, an offset given by a string like "foo", without warning was converted to 0 .

Example #13 Differences between PHP 5.3 and PHP 5.4

$str = "abc" ;

Var_dump($str["1"]);
var_dump (isset($str [ "1" ]));

Var_dump($str[ "1.0" ]);
var_dump (isset($str [ "1.0" ]));

Var_dump($str["x"]);
var_dump (isset($str [ "x" ]));

Var_dump($str[ "1x" ]);
var_dump (isset($str [ "1x" ]));
?>

The result of running this example in PHP 5.3 is:

string(1) "b" bool(true) string(1) "b" bool(true) string(1) "a" bool(true) string(1) "b" bool(true)

The result of running this example in PHP 5.4 is:

string(1) "b" bool(true) Warning: Illegal string offset "1.0" in /tmp/t.php on line 7 string(1) "b" bool(false) Warning: Illegal string offset "x" in / tmp/t.php on line 9 string(1) "a" bool(false) string(1) "b" bool(false)

Comment:

Attempting to access variables of other types (excluding arrays or objects that implement certain interfaces) with or {} silently return NULL.

Comment:

PHP 5.5 added support for accessing characters in string literals using the syntax or {} .

There are many useful functions for modifying strings.

The main functions are described in the string functions section, and for advanced search and replace, Perl-compatible regular expression functions.

Convert to string

The value can be converted to a string using a cast (string), or functions strval(). In expressions where a string is needed, the conversion occurs automatically. This happens when you use functions echo or print, or when the value of a variable is compared to a string. Reading the Types and Type Manipulation sections of the manual will make the following clearer. see also settype().

Arrays are always converted to string "array", so you can't display the contents of the array ( array) using echo or print to see what it contains. To view an individual element, use something like echo $arr["foo"]. See below for tips on how to display/view all content.

To convert a type variable "Object" per type string the magic method __toString is used.

Meaning NULL always converted to the empty string.

As you can see above, direct stringization of arrays, objects, or resources does not provide any useful information about the values ​​themselves, other than their types. A more appropriate way to output values ​​for debugging is to use functions print_r() and var_dump().

Most values ​​in PHP can be converted to a string for permanent storage. This method is called serialization and can be done with the function serialize().

Converting strings to numbers

If the string is recognized as a numeric value, the resulting value and type is determined as follows.

If the string does not contain any of the characters ".", "e", or "E", and the value of the number fits within the range of integers (defined PHP_INT_MAX), the string will be recognized as an integer ( integer). In all other cases, it is considered a floating point number ( float).

The value is determined by the initial part of the string. If the string starts with a valid numeric value, that value will be used. Otherwise, the value will be 0 (zero). A valid numeric value is one or more digits (which may contain a decimal point), optionally preceded by a sign, followed by an optional exponent. The exponent is "e" or "E" followed by one or more digits.

$foo = 1 + "10.5" ; // $foo is a float (11.5)
$foo = 1 + "-1.3e3" ; // $foo is a float (-1299)
$foo = 1 + "bob-1.3e3" ; // $foo is an integer (1)
$foo = 1 + "bob3" ; // $foo is an integer (1)
$foo = 1 + "10 Small Pigs" ; // $foo is an integer (11)
$foo = 4 + "10.2 Little Piggies" ; // $foo is a float (14.2)
$foo = "10.0 pigs" + 1 ; // $foo is a float (11)
$foo = "10.0 pigs" + 1.0 ; // $foo is a float (11)
?>

See the strtod(3) section of the Unix documentation for more information on this conversion.

If you want to test any of the examples in this section, copy and paste it and the following line to see what happens:

echo "\$foo== $foo ; type: " . gettype ($foo ) . "
\n" ;
?>

Don't expect to get the character code by converting it to an integer (as is done, for example, in C). To convert characters to their ASCII codes and vice versa, use the functions ord() and chr().

String Type Implementation Details

7 years ago

The documentation does not mention, but a closing semicolon at the end of the heredoc is actually interpreted as a real semicolon, and as such, sometimes leads to syntax errors.

$foo =<<abcd
END;
?>

This does not:

foo(<<abcd
END;
);
// syntax error, unexpected ";"
?>

Without semicolon, it works fine:

foo(<<abcd
END
);
?>

3 years ago

You can use string like array of char (like C)

$a = "String array test";

var_dump($a);
// Return string(17) "String array test"

var_dump($a);
// Return string(1) "S"

// -- With array cast --
var_dump((array)$a);
// Return array(1) ( => string(17) "String array test")

var_dump((array)$a);
// Return string(17) "S"

Norihiori

15 years ago

You can use the complex syntax to put the value of both object properties AND object methods inside a string. For example...
class Test(
public $one = 1 ;
public function two()(
return 2 ;
}
}
$test = new Test();
echo "foo ( $test -> one ) bar ( $test -> two ()) " ;
?>
Will output "foo 1 bar 2".

However, you cannot do this for all values ​​in your namespace. Class constants and static properties/methods will not work because the complex syntax looks for the "$".
class Test(
const ONE = 1 ;
}
echo "foo (Test::ONE) bar" ;
?>
This will output "foo (Test::one) bar". Constants and static properties require you to break up the string.

3 years ago

Beware that consistent with "String conversion to numbers":

If ("123abc" == 123 ) echo "(intstr == int) incorrectly tests as true.";

// Because one side is a number, the string is incorrectly converted from intstr to int, which then matches the test number.

// True for all conditionals such as if and switch statements (probably also while loops)!

// This could be a huge security risk when testing/using/saving user input, while expecting and testing for only an integer.

// It seems the only fix is ​​for 123 to be a string as "123" so no conversion happens.

?>

6 years ago

Leading zeroes in strings are (least-surprise) not treated as octal.
Consider:
$x = "0123" + 0;
$y = 0123 + 0;
echo "x is $x, y is $y"; //prints "x is 123, y is 83"
in other words:
* leading zeros in numeric literals in the source-code are interpreted as "octal", c.f. strtol().
* leading zeros in strings (eg user-submitted data), when cast (implicitly or explicitly) to integer are ignored, and considered as decimal, c.f. strtod().

10 years ago

Here is an easy hack to allow double-quoted strings and heredocs to contain arbitrary expressions in curly braces syntax, including constants and other function calls:

// Hack declaration
function_expr ($v ) ( return $v ; )
$_expr = "_expr" ;

// Our playground
define("qwe" , "asd");
define("zxc", 5 );

$a= 3 ;
$b= 4 ;

function c($a, $b) (return$a+ $b; }

// Usage
echo"pre{ $_expr(1 + 2 )} post\n"; // outputs "pre 3 post"
echo"pre{ $_expr(qwe)} post\n"; // outputs "pre asd post"
echo"pre{ $_expr(c($a, $b)+ zxc* 2 )} post\n"; // outputs "pre 17 post"

// General syntax is ($_expr(...))
?>

2 years ago

I though that it would be helpful to add this comment so that the information at least appears on the right page on the PHP site.

Note that if you intend to use a double-quoted string with an associative key, you may run into the T_ENCAPSED_AND_WHITESPACE error. Some regard this as one of the less obvious error messages.

An expression such as:

$fruit=array(
"a"=> "apple",
"b"=> banana,
//etc
);

print "This is a$fruit[ "a"]"; // T_ENCAPSED_AND_WHITESPACE
?>

will definitely fall to pieces.

You can resolve it as follows:

print"This is a$fruit[ a] " ; // unquote the key
print"This is a${ fruits[ "a"]} " ; // Complex Syntax
print"This is a{ $fruit[ "a"]} " ; // Complex Syntax variation
?>

I have a personal preference for the last variation as it is more natural and closer to what the expression would be like outside the string.

It's not clear (to me, at least) why PHP misinterprets the single quote inside the expression but I imagine that it has something to do with the fact quotes are not part of the value string - once the string is already being parsed the quotes just get in the way … ?

2 years ago

Both should work:(

classtesting{
public static
$var= "static";
public const VAR =
"const";

public function sayHelloStatic() {
echo
hello:{ $this:: $var} " ;
}

public function sayHelloConst() {
echo
hello:{ $this::VAR)" ; //Parse error: syntax error, unexpected ")", expecting "["
}
}

$obj=newtesting();
$obj-> sayHelloStatic();
$obj-> sayHelloConst();

3 years ago

Something I experienced which no doubt will help someone. . .
In my editor, this will syntax highlight HTML and the $comment:

$html =<<<"EOD"
$comment
EOD;

Using this shows all the same colors:

$html =<<$comment
EOD;

making it a lot easier to work with

11 years ago

To save Your mind don "t read previous comments about dates ;)

When both strings can be converted to the numerics (in ("$a" > "$b") test) then resulted numerics are used, else FULL strings are compared char-by-char:

var_dump("1.22" > "01.23" ); // bool(false)
var_dump("1.22.00" > "01.23.00" ); // bool(true)
var_dump("1-22-00" > "01-23-00" ); // bool(true)
var_dump((float)"1.22.00" > (float)"01.23.00" ); // bool(false)
?>



Tables:

C_id | company | location
1 | OOO Search | Kudykino field 15/3
2 | CJSC Elita | Slunysvalinsk 133/7
3 | OJSC Pyshpyshch | Soldatodachestroyskoe 404

Repair type (repair_types)
r_id | repair_types |
1 | Hammer + nails
2 | Beauty guidance
3 | Overhaul

List of orders (list)
l_id | Who | What need | time | operator comment
1 | 1 | 2 | %timestamp% | %operator_text%
2 | 2 | 1 | | %text%
3 | 3 | 2 | | %text%

Table #1 contains a list of clients.
Table #2 contains a list of services.
Table No. 3 contains a list of current orders for operational teams. This table is regularly updated and a web form is used to fill it. Since quite a lot of orders come in, records are entered in the table in the form of a client ID and a service.

Actually, the problem is that the third table should be displayed in the "order selection" web form, and instead of ID, you need to substitute the corresponding fields from other columns.

The code:
$query = "SELECT * FROM list";
$result = mysql_query($query);
while($row=mysql_fetch_array($result. // get results from each row
( echo "";// display data
}




CREATE TABLE "table_name" ("id" int(255) NOT NULL AUTO_INCREMENT, "list" text (80) NOT NULL, PRIMARY KEY("id".

INSERT INTO "table_name" ("list") VALUES ("bla-bla")



best answer $query =
"
select companies.Company, repair_types.Repair_types, list.comment_l
from list
inner join companies
on list.Who = companies.c_id
inner join repair_types
on list."What need" = repair_types.r_id
";

Of course, images, sound files, video information, animation data, and applets make up an important part of the content of the World Wide Web, but the vast majority of data on the Web is still in the form of text - sequences of characters like this sentence. The basic PHP data type for representing text is a string.

Description of strings in PHP

Strings are sequences of characters that can be treated as a single entity - assigned to variables, passed as input to functions, returned from functions, or sent as output to be displayed on the user's web page. The simplest way to specify a string in PHP code is to enclose a sequence of characters in quotes, single ("") or double ("), for example, as follows:

$myString = "Simple string"; $anotherString = "Another string";

The difference between single quotes and double quotes is due to how extensively the PHP environment interprets the quoted characters before creating the actual string. If the string is enclosed in single quotes, then almost no interpretation is performed, and if the string is enclosed in double quotes, then the PHP environment performs the substitution of the values ​​of all variables that are specified in the string, and also replaces some special character sequences that begin with the character backslash ().

For example, after processing the following code that is part of a web page:

$count = 13; $string1 = "The string \"Hello, world!\" contains $count characters.
"; $string2 = "The string \"Hello, world!\" contains $count characters.
"; echo $string1; echo $string2;

you can expect to get the following output in the browser window:

Value substitution using curly braces

In most situations, you can simply enclose the variable in a double-quoted string, and the value of the variable will be substituted into the string when the interpreter processes the string. But in the two situations described below, it may turn out that the string interpreter is not able to make an informed decision and needs additional guidance from the developer.

The first situation is that the interpreter cannot determine where the variable name ends, and the second situation occurs when it is necessary to substitute in the string not the value of a simple variable, but the value of an expression. In these cases, the developer can clarify by enclosing the name or expression to be processed in curly braces (()).

For example, the PHP interpreter has no problem processing the following code:

$sport = "volleyball"; $play = "I love playing $sport.";

In such cases, the interpreter encounters the $ character, then starts substringing the characters to reveal the variable name, and doing so until it encounters a space or period that follows the variable name $sport. Spaces and dots cannot be part of a variable name, so it becomes clear that the variable in question is named $sport; the PHP interpreter then successfully finds a value for the given variable ("volleyball") and substitutes it.

But sometimes it is not possible to mark the end of a variable name with a space or a dot. Consider the following example:

$sport1 = "will"; $sport2 = "foot"; $sport3 = "basket"; // Incorrect constructs $play1 = "I love playing $sport1ball."; $play2 = "I love playing $sport2ball."; $play3 = "I love playing $sport3ball.";

In this case, the desired effect will not be achieved, because the PHP interpreter will consider the string $sport1 as part of the variable name $sport1bol, which, apparently, has not been assigned any value. Instead, you need to use the following notation:

// Valid construction $play1 = "I like to play ($sport1)ball."; $play2 = "I love playing ($sport2)ball."; $play3 = "I love playing ($sport3)ball.";

This notation tells the PHP interpreter that only the value of the expression with the variable name enclosed in curly braces needs to be evaluated before value substitution.

For similar reasons, the PHP interpreter, if curly braces are not used, has difficulty substituting the values ​​of complex expressions with variable names, such as accessing elements of multidimensional arrays and object variables. The general rule is that if an opening curly brace (() is immediately followed by a $, then the PHP interpreter evaluates the value of the expression with the variable name up to the closing curly brace ()), and then substitutes the resulting value into the string. (If you want a literal value ($) to appear in the string, this can be done by prefixing each of these characters with a backslash, \).

Characters and character indices in strings

Unlike some other programming languages, PHP does not have a separate character type that is not the same as the string type. Generally speaking, functions that require character actual parameters in other languages ​​are designed to take strings of length 1 in PHP.

Selecting individual characters from a string can be done by specifying a zero-based ordinal character, which must be specified in curly braces immediately after the name of the string variable. Such characters are actually single-character strings. For example, running the following code:

$myString = "Doubled"; for ($index = 0; $index< strlen($myString); $index++) { $char = $myString{$index}; print("$char$char"); }

results in the following output in the browser window:

Obviously, each character of the string is printed twice on each pass through the loop. The strlen() function returns the length of a string.

String Operations

PHP provides two string operations: the dot operator (.), or concatenation operator, and the dot and equal sign operator (.=), or concatenation and assignment operator. Consider the following example:

$string1 = "This is a part"; $string2 = "strings"; // String concatenation echo $string1." simple ".$string2."
"; // "This is part of a simple string" // Concatenate and assign $string1 .= " simple "; // Equivalent to $string1 = $string1." simple "; $string1 .= $string2; echo $string1; // "This is part of a simple string"

It should be noted that in the example above, the first echo statement is not passed multiple string actual parameters; only one string actual parameter is passed, resulting from the concatenation of four strings. The first and third lines are set using variables, and the second and fourth lines are a literal string enclosed in double quotes.

Subdocument Syntax Structure (Heredoc)

In addition to the syntactic structures of strings in single and double quotes, PHP provides another way to specify a string, called the syntactic structure of the nested document (Heredoc). As it turned out, such a syntactic structure is a very convenient means of specifying large fragments of text substituted using variables, since it eliminates the need for the user to designate internal quotes using special characters. This method is especially useful when creating pages that contain HTML forms.

The operator sign used in the syntactic structure of the subdocument is (<<<). За этим знаком должна непосредственно следовать метка (не заключенная в кавычки), которая обозначает начало многострочного текста. Интерпретатор PHP продолжает включать в состав значения переменной следующие строки до тех пор, пока снова не появится эта же метка в начале строки. За заключительной меткой может следовать необязательная точка с запятой, а какие-либо другие символы после метки не допускаются.

Consider the following example:

$string =<<

EOT; echo $string;

Note that the final word EOT shown above should not be indented at all, otherwise it will be treated as belonging to the additional included text. It is not necessary to use the word "EOT" as a label, the label can have any name, following the usual PHP variable naming conventions.

Variable substitution is done using exactly the same method as when using strings enclosed in double quotes. A convenient feature of a subdocument is that quotation marks can be entered into the text designated in this way without the use of any control characters, and this does not lead to premature completion of the line formation. The example shown above outputs a simple HTML form:

String Functions

The PHP language provides a wide variety of functions for processing and converting strings. If you ever have the need to create your own function that reads and processes strings character by character to form a new string, first consider if someone else might have faced a similar task before. And if intuition suggests that such a possibility exists, then perhaps there is a built-in function that solves the problem. For more information about string functions, please refer to https://php.net/manual/en/ref.strings.php .

This section introduces the basic functions for checking, comparing, modifying, and printing strings. In order to really master PHP's string manipulation facilities, it seems that one should have at least a cursory familiarity with everything covered in this section. A description of the functions designed to work with regular expressions can be found in the following article.

String validation

What are the most common questions about strings that need to be answered? First on the list of questions is the question of how long a string is; to answer it, the strlen() function is used, the name of which is an abbreviation for string length - the length of the string. An example of using such a function is shown below:

$enStr = "Hello world!"; $rusStr = "Simple string"; echo $enStr." - ".strlen($enStr)." characters
"; echo $rusStr." - ".strlen($rusStr)." characters";

Running this code produces the following ambiguous output:

As you can see, for the string "Hello world!" the result is correct, but for the string "Simple string" the result of 27 characters is incorrect. What is the matter here? The fact is that the strlen() function counts bytes, not characters. In the first case, all characters in the string are in English, i.e. are represented by ASCII encoding and encoded as 1 byte. In the second case, the string contains Russian characters, which are encoded in 2 bytes (UTF-8). To avoid problems later when working with string functions, PHP should use functions for multibyte encodings that start with the mb_ prefix. This means that in the previous example, you need to replace the strlen() function with mb_strlen() and explicitly specify the encoding:

Echo $rus_str." - ".mb_strlen($rus_str, "UTF8")." characters";

Finding characters and substrings

The next question regarding strings is what they contain. For example, the strpos() function allows you to find the position number of a particular character in a string, if any:

$enStr = "Hello world!"; echo "Symbol "l": ".strpos($enStr, "l"); // 2

Situations involving the use of the strpos() function are cases in which PHP's type insensitivity can be a problem. If a match is not found, then the function returns a false value, and if the desired character matches the very first character in the string, then the function returns 0 (because the character positions in the string start from 0, not from 1). Both of these values ​​correspond to false when used to test a boolean condition. One way to distinguish between these values ​​is to use the identity comparison operator (the === operator, introduced since PHP4), which returns true only if its operands are the same and are of the same type. The identity comparison operator can be used to test whether the return value is 0 (or false) without the risk of confusing the return value with other values ​​that might become the same after the cast.

The strpos() function can also be used to search for a substring rather than a single character. To do this, it is enough to specify a multi-character string, and not a single-character string, as the desired one. In addition, in the call to this function, you can specify an additional integer parameter that specifies the position of the start of the search.

It is also possible to search in the opposite direction, from the end of the string to the beginning. The strrpos() function is used for this. (Note that this function has an extra r in its name, which is short for reverse.) This function takes as parameters the string to search and the single-character string to search for, and then returns the last position occurrences of the second parameter in the first parameter. (Unlike the strpos() function, in the strrpos() function, the searched string must consist of only one character.) After applying this function with the same parameters as in the previous example, another position will be found:

$enStr = "Hello world!"; echo "Symbol "l": ".strrpos($enStr, "l"); // 9 because search is from the end of the string

Comparison

Does that line match this line? Apparently, code often has to answer this question as well, especially when it comes to handling input from the end user.

The simplest method for finding the answer to the question of whether strings are the same is to use the simple equality comparison operator (==), which checks for equality not only of strings, but also of numbers. When using the == operator, two strings are considered the same if they contain exactly the same character sequences. This does not check for a more stringent definition of identical strings, such as the condition that these strings are stored at the same address in memory, but is case-sensitive (in other words, whether the compared letters are uppercase or lowercase) .

The results of comparing two strings using the == operator (or the corresponding operators< и >) can only be trusted if both operands are strings and it is known that no type conversion has occurred. And the results of the check using the strcmp() function, which is described below, can always be trusted.

The most important string comparison function that does most of the work is strcmp(). This function takes two strings as parameters and compares byte by byte until it finds a difference. The function then returns a negative number if the first string is less than the second, and a positive number if the second string is less than the first. If the strings are identical, the strcmp() function returns zero.

The strcasecmp() function works in the same way, except that the comparison for equality is case-insensitive. For example, a call to strcasecmp("hey!", "HEY!") should return zero.

Search

The comparison functions just described let you know if one string is equal to another string. And to determine whether one string is contained within another, use the strpos() function described above, or the strstr() function and similar functions.

The strstr() function takes as parameters the string to be searched and the string to be searched (in that order). If successful, this function returns the part of the string that begins with the first occurrence of the search string (and includes the search string). If no such string is found, the function returns false. The following code snippet provides examples of how to use this function:

$str = "Hello world!"; $findStr = "world"; echo "Substring " $findStr" in the original string: ".strstr($str, $findStr);

Like the strcmp() function, the strstr() function has a case-insensitive version called stristr() (the i in that name is short for insensitive). The stristr() function is identical to the strstr() function in all respects, except that the comparison treats lowercase letters as their uppercase counterparts.

Substring selection

Many PHP string functions perform cut and paste operations on strings. A cut is a selection of a part of a string, and an insertion is a selective modification of a string. Keep in mind that even the insert functions (most often) do not modify the string given as an input parameter. Typically, such functions return a modified copy and leave the original parameter unchanged.

The simplest way to sample part of a string is to use the substr() function, which returns a new string containing part of a subsequence of characters from the old string. The substr() function takes as parameters a string (from which the substring is to be sampled), an integer (the position at which the desired substring starts), and an optional integer third parameter that specifies the length of the desired substring. If the third parameter is not specified, then the substring is assumed to continue to the end of the string. (It should be remembered that when using this function, the numbering of positions in a line starts from zero, and not from one, as in all PHP function parameters that indicate numerical positions in strings.) For example:

$str = "Hello world!"; echo mb_substr($str, 7, 3, "UTF8"); // "world"

Both the start position parameter and the length parameter can be negative, but in both these cases, a negative value has a different meaning. If the starting position is negative, then the position of the starting character of the substring is determined by counting backwards from the end of the string, rather than counting forward from the beginning of the string. (A starting position of -1 indicates that the count starts from the last character, a value of -2 indicates the penultimate character, and so on.)

Based on this, one might assume that a negative length value also implies that the substring should be determined by counting backwards from the start character, rather than counting forward, but this is not the case. The assertion that the character in the starting position is the first character in the returned string (and not the last) is always true. Instead, a negative length parameter means that the ending character is determined by counting backwards from the end, rather than counting forward from the starting position.

Below are some examples with positive and negative parameters:

$str = "Hello world!"; echo mb_substr($str, 7, 3, "UTF8")."
"; // "world" echo mb_substr($str, -4, 3, "UTF8")."
"; // "world" echo mb_substr($str, 0, -5, "UTF8")."
"; // "Hi"

Removing spaces and tabs from strings

Technically, the chop(), ltrim(), and trim() functions are functions for working with substrings (which are much like other functions), but in fact, these functions are designed to remove unwanted characters from strings. The chop(), ltrim(), and trim() functions, respectively, remove trailing, leading, leading, and trailing whitespace characters from a string given as the only string parameter.

In addition to spaces, these functions remove other whitespace characters such as those indicated by the escape sequences \n, \r, \t, and \0 (end-of-line characters, tabs, and null characters used to mark the end of a line in C programs).

In PHP, it is common to use the function to remove whitespace at the end of a string, called chop(), but an identical function, more descriptively named rtrim(), can also be called. Finally, it should be noted that the chop() function, despite the fact that its name, which means "chop off", sounds very menacing, does not damage the original $original parameter, which retains the previous value.

String replacement

All of the string functions discussed above provided for fetching part of the input parameter, rather than forming a completely new string. This section discusses the str_replace() and substr_replace() functions for this purpose.

The str_replace() function allows you to replace all occurrences of a given specific substring with another string. This function takes three parameters: the string to be searched, the substring to be replaced after it is found, and the string to be used for the replacement. Consider the following example:

$str = "Hello world!"; echo str_replace("world", "planet", $str); // "Hello planet!"

The replacement is performed on all occurrences of the substring found in the search string. If the above example were used to replace the name of a city in an obsolete encyclopedia, then after converting the entire text of the encyclopedia into a single PHP line, such a replacement in the entire text could be performed in one pass.

As shown above, the str_replace() function selects the part of the source string to be replaced by looking for occurrences of the desired substring in the source string; in contrast, substr_replace() selects the part to be replaced by its absolute position. This function takes up to four parameters: the string to be replaced, the string to be replaced, the starting position of the replacement, and (as an optional parameter) the length of the string to be replaced. Consider the following example:

echo substr_replace("ABCDEFG", "-", 2, 3); // "AB-FG"

The CDE portion of the string has been replaced with a single character. Note that in this case it was allowed to replace a substring with a string of a different length. If the length parameter is omitted, then the entire part of the string after the start position is assumed to be replaced.

The substr_replace() function also accepts negative parameters as a starting position and length, which are treated exactly as in the substr() function above. It is important to remember that as a result of operations performed using the str_replace and substr_replace functions, the original string remains unchanged.

Finally, there are a number of less widely used functions that form new strings from old ones. The strrev() function simply returns a new string with the characters in the input string reversed. The str_repeat() function takes one string and one integer parameter and returns a string containing the specified number of copies of the string parameter:

echo str_repeat("ABC", 3); // ABCABCABC

Case conversion functions

These functions allow you to convert lowercase letters to uppercase and vice versa. The strtolower() function returns a string with all letters converted to lowercase. It does not matter whether the original string contained only uppercase letters or uppercase and lowercase. For example:

$str = "Hello world!"; echo mb_strtolower($str, "UTF8"); // "Hello World!"

If you've already experienced the need to do a lot of form validation, you might have noticed that the strtolower() function is an extremely handy tool for handling email addresses received from users who still don't know that the email addresses are case-insensitive. No less useful are other functions related to this category.

The strtoupper() function returns a string with all letters converted to uppercase. An example is the following code snippet:

$str = "Hello world!"; echo mb_strtoupper($str, "UTF8"); // "HELLO WORLD!"

The ucfirst() function capitalizes only the first letter of the string, the ucwords() function capitalizes the first letter of every word in the string. Neither the ucwords() nor ucfirst() function has a similar function for multibyte encoding, so they are not compatible with Cyrillic strings.

Functions for introducing control characters

One of the advantages of the PHP language is that it can be used to exchange data with almost any system. Means of this kind are usually considered as a kind of "software glue". In this role, the PHP language is used to interact with database servers, LDAP servers, to exchange data via sockets and the HTTP connection itself. Often this interaction is done by first creating a message string (like a database query) and then passing that message to the receiving program. But programs often give special meaning to some characters, and therefore they have to be converted to control characters. This means that the receiving program is instructed to treat such characters as a literal part of the string, rather than apply special treatment to them.

Many users, to deal with this problem, enable the use of the so-called "magic quote mode", which ensures that quotes are converted to control characters before strings are inserted into databases. But if such a mode of processing is not feasible or desirable, then one has to use the good old means of inserting backslash characters and then deleting those characters.

The addslashes() function converts single and double quotes, backslashes, and null characters to escape sequences using backslashes, since these are the characters that typically need to be converted to escape sequences when preparing queries against databases:

$escapedstring = addslashes("String with "quotes"."); $query = "INSERT INTO test (quote) values ​​("$escapedstring")"; $result = mysqli_query($link, $query) or die(mysql_error());

Running this code prevents the SQL statement from being misinterpreted as if the string ended right before the letter "k". And after fetching this data, you need to use the stripslashes() function to remove backslashes.

The quotemeta() function converts a wider set of characters into escape sequences. All of these characters usually have special meanings on the Unix command line: " . ", " " ", " + ", " * ", " ? ", " [ ", " ] ", " ^ ", " (", " $ " and ")". For example, running the following code:

$str = "These characters ($, *, ^) need to be converted."; echo quotemeta($str);

outputs this line:

Output functions to an external device and to a line

The main constructs used for output are print and echo, which were discussed in detail earlier. The standard way to output the values ​​of variables to an external device is to include the names of these variables in a double-quoted string (which is processed by the interpreter to substitute the values ​​of the variables), and then pass this string to the print or echo construct.

If you need even more precisely formatted output, you can use PHP's printf() and sprintf() functions. These two functions take the same parameters: a special format string followed by an arbitrary number of other parameters that are substituted in the correct places in the format string to produce a result. The only difference between printf() and sprintf() is that printf() sends the result string directly to the external device used for output, while sprintf() returns the result string as the result of its execution.

A few words for experienced C programmers. This version of sprintf() differs slightly from the C version in that sprintf() does not need to provide an allocated string to write to, since the PHP interpreter allocates memory for the resulting string on behalf of the user.

The main difficulty associated with using these functions is the correct definition of the format string. Each character in the format string appears directly in the resulting value, except for the % characters and the characters that immediately follow those characters. The % symbol marks the beginning of a conversion specification, which specifies how to output to an external device one of the parameters that follows the format string.

After the % sign, there are five elements that make up the conversion specification, which are described below, some of which are optional: padding, alignment, minimum width, precision, and type:

  • An optional minus sign (-) is used to indicate whether a number is negative.
  • The single (optional) fill character is either 0 or a space (). This symbol is used to fill any space that would otherwise be left blank, but which the user insisted on selecting (by setting the minimum width parameter too high). If this padding character is not specified, it defaults to padding with spaces.
  • The optional justification character (-) specifies whether the output value should be left-justified or right-justified. If this character is specified, then the value will be left-justified, and if it is not present, right-justification is applied.
  • An optional minimum-width numeric value that specifies the minimum number of positions the output value should occupy. (If the output values ​​require more character positions than specified, then the output value is out of range.)
  • An optional precision specifier, formatted as a dot (.) followed by a number. The specifier indicates with what precision, measured by the number of decimal places after the point, a double-precision floating-point number should be output. (The application of this specification has no effect on the output of data other than double-precision floating-point numbers.)
  • A single character indicating how the value type should be interpreted. The f character indicates that the value should be printed as a double-precision floating point number, the s character indicates that the value should be printed as a string, and the remaining possible characters (b, c, d, o, x, X) indicate that that the value should be interpreted as an integer and output in various formats. These formats are b for output in binary format, c for output of a character with the corresponding ASCII code value, o for output in octal format, x for output in hexadecimal format (with lowercase lettering of digits). ), and X for displaying hexadecimal numbers that use uppercase letters as literal symbols for digits.

The following is an example of outputting the same double-precision floating point number in several different ways:

%10f 
%-10f
%2.2f", $value, $value, $value, $value); ?>

It produces the following results:

The construction used in this example

...
is an HTML descriptor that tells the browser that the block enclosed in this descriptor should be formatted literally, without compressing multiple spaces into one, etc.

Functions for working with HTML code

PHP provides a number of functions for manipulating strings containing web-specific data. An overview of these features is provided in the table below:

String functions designed to work with HTML code

Function Description
htmlspecialchars() It takes a string as a parameter and returns a string in which four characters that have a special meaning in the HTML language have been replaced with special strings. Each of these characters is replaced by the corresponding HTML component, which is replaced again by the original character when the page text is expanded in the browser. The & character is replaced by the & character " (double quote character) component - by the " character< - < а символ > - >
htmlentities() Performs more complete processing than htmlspecialchars(), i.e. replaces with the HTML component not only special characters, but also all characters for which the replacement by the HTML component is provided
get_html_translation_table() Takes one of two special constants (HTML_SPECIAL_CHARS or HTML_ENTITIES) and returns a conversion table used by the htmlspecialchars() or htmlentities() functions, respectively. The lookup table is an array whose keys are character strings and whose corresponding values ​​are strings to replace them.
nl2br() Takes a string as a parameter and returns the same string, but with descriptors
, inserted before all end-of-line characters (\n, \r, or \r\n). The need to use this function arises, for example, if you want to provide the same paragraphing of the text displayed in the browser as in the source text
strip_tags() Takes a string as a parameter and does its best to form a string stripped of all HTML descriptors and all PHP descriptors

Hashing data using the MD5 algorithm

The MD5 algorithm is a string processing algorithm that is used to generate a so-called digest, or digital signature, for any string passed as a parameter. The algorithm generates a string of constant length based on the input string, consisting of 32 hexadecimal digits (0-9, a-f). The results generated by the MD5 algorithm have very useful properties, as described below:

  • The MD5 algorithm always ensures that the output string is the same when given the same input string, so MD5 encryption cannot be used to store passwords.
  • The results of applying the MD5 algorithm have a fixed length and are very evenly distributed over the entire range of possible values.
  • An input string can be generated that corresponds to a given output string of the MD5 algorithm, or two input strings can be created, the processing of which would lead to the same output string, but only under certain conditions.

PHP's implementation of the MD5 algorithm is available as the md5() function, which takes a string as input and produces the results as a 32-character digest. For example, running the following code:

$str = "Hello world!"; echo "Hash code for string "$str": ".md5($str)."
"; $str = "Hello, world!"; echo "Hash code for string "$str": ".md5($str)."
"; $str = "Hello world"; echo "Hash code for string "$str": ".md5($str)."
";

results in the following results in the browser window:

Of course, in this case, all the input strings are very similar to each other, but the output strings do not have any visible similarity. In addition, the range of possible output values ​​is extremely large (1632), so the likelihood of two different strings being processed (which would produce the same MD5 value) is extremely unlikely.

Due to the above characteristics of the MD5 algorithm, the values ​​obtained with its help can be used to solve a wide variety of problems, including those described below:
Calculating the checksum of a message or file
To check if the message was corrupted during transmission, you can send an MD5 digest along with the message, and regenerate the MD5 digest when the message is received. If the two versions of the digest do not match, then corruption was made during transmission.
Control over whether the contents of a file remain unchanged
This task is similar to the task of calculating the checksum. The MD5 algorithm is often used to perform this operation in search engines if it is required to periodically check whether a web page has changed and re-index if necessary. The fact is that for further verification it is much easier to organize the storage of the MD5 digest than the entire source file.
Splitting multiple strings or files into subsets
To solve the problem of splitting a set of strings into N randomly selected subsets, you can calculate the MD5 digest of each string, take the first few hexadecimal characters, convert them to a number, get the modulo modulo of that number, and use that remainder as the subset number, in which this line should be written.

In addition to the md5() function, PHP provides the md5_file() function, which takes a file name as a parameter and returns a hashed MD5 value corresponding to the contents of the file.

Functions designed to evaluate the similarity of strings

In practice, it often becomes necessary to determine how similar two strings are. Obviously, the results of string similarity estimation depend on what is meant by the concept of string similarity.

If similarity in spelling is considered as a criterion for evaluating similarity, then one can apply Levenshtein metric. The levenshtein() function takes two strings as parameters and returns the minimum number of character addition, deletion, and replacement operations required to convert one string to another. Consider an example:

echo levenshtein("Tim", "Time"); // 1 echo levenshtein("boy", "chefboyardee"); // 9 echo levenshtein("never", "clever"); // 2

If phonetic similarity is considered as a similarity criterion, then the soundex() and metaphone() functions can be used to evaluate similarity. Both of these functions take the string in question as input and return a key string indicating the pronunciation category of the given word (which is treated as an English word). If two words used as input string content correspond exactly to the same output value, then they are likely to be pronounced the same.

Parsing and tokenizing functions

Sometimes it becomes necessary for a program to break strings into components, guided by its own definition of what should be considered a component. The process of splitting a long string into parts is called tokenization. In particular, such a process is part of the general procedure for interpreting or compiling any computer program, including a program written in PHP. The PHP language provides a special function for this purpose - strtok ().

The strtok() function takes two parameters: a string to tokenize, and a string containing all delimiters (characters that are treated as boundaries between tokens). The first time it is called, both parameters are used and the function returns a string value representing the first token. To select subsequent tokens, the same call is made, but the source string parameter is omitted. The function remembers the address of the string given in the first parameter and uses it as the current string. In addition, this function remembers where processing was terminated in the previous call. Consider the following example:

$token = strtok("open-source HTML-embedded server-side Web scripting", " "); while($token) ( echo $token."
"; $token = strtok(" "); )

which results in the following output in the browser window:

The original string is split at the location where each space is located.

The strtok() function builds tokens one by one. You can also use the explode() function, which performs roughly the same thing, except that it stores all the tokens at once in one array. After receiving the tokens, represented as an array, you can perform any operations with them, including sorting.

The explode() function takes two parameters: a delimiter string and a string to be split into tokens. This function returns an array, each element of which is a substring between instances of the delimiter in the string to be split. Consider the following example:

$explodeResult = explode("AND", "one AND a two AND a three");

which results in an array $explode_result containing three elements, each of which is a string: "one", "a two", and "a three". In this particular example, no uppercase letters occur in any of the strings contained in the array because the AND separator is not present in the result.

The delimiter string used in the explode() function is quite different from the delimited string used in the strtok() function. The delimiter is a complete string, so all of the characters in that string must be found in the source string in the same order as in the delimiter for the delimiter to be considered found.

On the other hand, a delimited string in the strtok() function specifies many individual characters, each of which is treated as a delimiter. This means that the explode() function is more selective, but more prone to breakage. In particular, if a long string accidentally misses even a single space or an end-of-line character that is part of the delimiter, then the entire operation of this function may be broken.

The explode() function has an inverse function, implode(), that takes two parameters: a connecting string (similar to the separator string in the explode() function) and an array of strings, similar to the one returned by the explode() function. The implode() function returns a string created by inserting a connecting string between all consecutive string elements in an array.