Expectedly <?php $string[$x] ?> and <?php substr($string, $x, 1) ?> will yield the same result... normally!
However, when you turn on the Function Overloading Feature (http://de.php.net/manual/en/mbstring.overload.php), this might not be true!
If you use this Overloading Feature with 3rd party software, you should check for usage of the String access operator, otherwise you might be in for some nasty surprises.
Les chaînes de caractères
Une chaîne de caractères est une série de caractères. Avant PHP 6, un caractère est la même chose qu'un octet. Ainsi, il y a exactement 256 caractères différents. Ceci impliquait également que PHP n'avait pas de support natif pour l'unicode. Voir les fonctions utf8_encode() et utf8_decode() pour des fonctionnalités Unicode basiques.
Note: Ce n'est pas un problème pour une chaîne de caractères de devient très grande. PHP n'impose pas de taille à une chaîne de caractères ; la seule limite est la mémoire disponible sur le système sous lequel PHP s'exécute.
Syntaxe
Une chaîne de caractères littérale peut être spécifiée de 4 façons différentes :
- Entourée de simple guillemet
- Entourée de guillemet double
- syntaxe heredoc
- syntax nowdoc (depuis PHP 5.3.0)
Entourée de simple guillemet
La façon la plus simple de spécifier une chaîne de caractères est de l'entourer de guillemet simple (le caractère ').
Pour spécifier un guillemet simple littéral, vous devrez l'échapper d'un antislash (\). Pour spécifier un antislash littéral avant un guillemet simple, ou à la fin d'une chaîne de caractères, échappez-le deux fois (\\). Notez que si vous tentez d'échapper n'importe quel autre caractère, l'antislash s'affichera.
Note: Contrairement aux 2 autres syntaxes, les variables et les séquences échappées des caractères spéciaux ne seront pas traitées lorsqu'elles seront dans une chaîne de caractères entourée de simple guillemet.
<?php
echo 'ceci est une chaîne simple';
echo 'Vous pouvez également ajouter des nouvelles lignes
dans vos chaînes
de cette façon';
// Affiche : Arnold dit : "I'll be back"
echo 'Arnold dit : "I\'ll be back"';
// Affiche : Voulez-vous supprimer C:\*.*?
echo 'Voulez-vous supprimer C:\\*.*?';
// Affiche : Voulez-vous supprimer C:\*.*?
echo 'Voulez-vous supprimer C:\*.*?';
// Affiche : Ceci n'affichera pas \n de nouvelle ligne
echo 'Ceci n\'affichera pas \n de nouvelle ligne';
// Affiche : Les variables ne seront pas $traitees $ici
echo 'Les variables ne seront pas $traitees $ici';
?>
Entourée de guillemet double
Si la chaîne de caractères est entourée de guillemet double ("), PHP interprétera plus de séquences échappées pour les caractères spéciaux :
| Séquence | Signification |
|---|---|
| \n | Fin de ligne (LF ou 0x0A (10) en ASCII) |
| \r | Retour à la ligne (CR ou 0x0D (13) en ASCII) |
| \t | Tabulation horizontale (HT or 0x09 (9) en ASCII) |
| \v | Tabulation verticale (VT or 0x0B (11) en ASCII) (depuis PHP 5.2.5) |
| \f | Saut de page (FF ou 0x0C (12) en ASCII) (depuis PHP 5.2.5) |
| \\ | Antislash |
| \$ | Signe dollars |
| \" | Guillemet double |
| \[0-7]{1,3} | La séquence de caractères correspondant à une expression régulière est un caractère, en notation octal |
| \x[0-9A-Fa-f]{1,2} | La séquence de caractères correspondant à une expression régulière est un caractère, en notation hexadécimale |
De la même façon que pour les chaînes entourées de simple guillemet, l'échappement de tout autre caractère affichera l'antislash. Avant PHP 5.1.1, l'antislash de \{$var} été également affiché.
La fonctionnalité la plus intéressante des chaînes entourées de guillemet double est que les noms de variables sont traités. Voir la documentation sur l'analyse des chaînes de caractères pour plus de détails.
Syntaxe Heredoc
Une 3ème façon de délimiter une chaîne de caractères est la syntaxe Heredoc : <<<. Après cet opérateur, un identifiant est fourni, puis, une nouvelle ligne. La chaîne elle-même suit, puis, le même identifiant pour fermer la notation.
L'identifiant doit commencer la première colonne de la ligne. De plus, l'identifiant doit suivre les mêmes règles que n'importe quel libellé PHP : il ne doit contenir que des caractères alphanumériques et des soulignés, et doit commencer par un caractère non numérique ou un souligné ("underscore").
Il est très important de noter que la ligne contenant l'identifiant ne doit contenir aucun autre caractère, mise à part, éventuellement, un point-virgule (;). Cela signifie que l'identifiant ne doit pas être indenté, et il ne doit y avoir aucun espace ou tabulation avant ou après le point-virgule. Il est également important de garder à l'esprit que le premier caractère avant l'identifiant de fermeture doit être une nouvelle ligne sur les systèmes Unix, incluant Max OS X. Le délimiteur de fermeture (pouvant être suivi d'un point-virgule) doit aussi être suivi d'une nouvelle ligne.
Si cette règle n'est pas respectée et que l'identifiant de fermeture n'est pas "propre", il ne sera pas considéré comme identifiant de fermeture, et PHP continuera à en chercher un. Si un identifiant de fermeture "propre" n'est pas trouvé avant la fin du fichier courant, une erreur d'analyse sera émise à la dernière ligne.
Heredocs ne peut être utilisé pour initialiser les membres d'une classe. Utilisez la syntaxe nowdocs à la place.
Exemple #1 Exemple invalide
<?php
class foo {
public $bar = <<<EOT
bar
EOT;
}
?>
Heredoc se comporte exactement comme une chaîne entourée de double guillemet, sans avoir de double guillemet. Cela signifie que les guillemets dans une syntaxe Heredoc n'ont pas besoin d'être échappés, mais les codes d'échappement listés ci-dessus peuvent toujours être utilisés. Les variables seront traitées mais les mêmes attentions doivent être prises lorsque vous utilisez des variables complexes dans une syntaxe Heredoc.
Exemple #2 Exemple de chaînes Heredoc
<?php
$str = <<<EOD
Exemple de chaîne
sur plusieurs lignes
en utilisant la syntaxe heredoc.
EOD;
/* Exemple plus complexe, avec des variables. */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
Mon nom est "$name". J'affiche quelques $foo->foo.
Maintenant, j'affiche quelques {$foo->bar[1]}.
Et ceci devrait afficher un 'A': \x41
EOT;
?>
L'exemple ci-dessus va afficher :
Mon nom est "MyName". J'affiche quelques Foo. Maintenant, j'affiche quelques Bar2. Et ceci devrait afficher un 'A': A
Note: Le support de la syntaxe Heredoc a été ajouté en PHP 4.
Nowdoc
Nowdocs est aux chaînes entourées de guillemet simple ce qu'heredocs est aux chaînes entourées de guillemet double. nowdoc est spécifié de manière similaire à heredoc, mut aucune analyse n'est effectuée dans une syntaxe nowdoc. La construction est idéale pour embarquer du code PHP ou d'autres larges blocs de texte, sans avoir besoin d'échapper quoi que ce soit. Cette syntaxe partage les mêmes fonctionnalités que le constructeur SGML <![CDATA[ ]]>, en ce qu'elle déclare un bloc de texte qui ne doit pas être analysé.
Nowdoc est identifié avec la même séquence <<< utilisée par heredocs, mais l'identifiant qui suit est entouré de guillemet simple,e.g. <<<'EOT'. Toutes les règles concernant les identifiants heredoc sont également appliquer aux identifiants nowdoc, et tout spécialement, celles concernant l'apparence de l'identifiant.
Exemple #3 Exemple de chaînes Nowdoc
<?php
$str = <<<'EOD'
Exemple de chaîne
sur plusieurs lignes
en utilisant la syntaxe nowdoc.
EOD;
/* Exemple complexe, avec des variables. */
class foo
{
public $foo;
public $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<'EOT'
Mom nom est "$name". J'affiche quelques $foo->foo.
Maintenant, j'affiche quelques {$foo->bar[1]}.
Ceci ne devrait pas afficher un 'A': x41
EOT;
?>
L'exemple ci-dessus va afficher :
Mom nom est "$name". J'affiche quelques $foo->foo. Maintenant, j'affiche quelques {$foo->bar[1]}. Ceci ne devrait pas afficher un 'A': \x41
Note: Contrairement à heredocs, nowdocs peut être utilisé dans n'importe quel contexte de données statiques. L'exemple typique est l'initialisation de membres ou de constantes de classe :
Exemple #4 Exemple avec des données statiques
<?php
class foo {
public $bar = <<<'EOT'
bar
EOT;
}
?>
Note: Le support Nowdoc a été ajouté en PHP 5.3.0.
Analyse des variables
Lorsqu'une chaîne de caractères est spécifié en guillemet double ou en heredoc, les variables sont analysées.
Il y a 2 types de syntaxe : une simple et une complexe. La syntaxe simple est la plus commune et la plus pratique. Elle fournit une façon d'embarquer une variable, un tableau ou un objet dans une chaîne avec un minimum d'effort.
La syntaxe complexe a été introduite en PHP 4, et se reconnaît en l'utilisation d'accolades autour de l'expression.
Syntaxe simple
Si un signe dollars ($) est rencontré, l'analyse prendra autant de caractères que possible pour former un nom de variable valide. Si vous entourez un nom de variable par des accolades explicitement, alors le nom de la variable n'aura aucune ambiguïté.
<?php
$beer = 'Heineken';
echo "$beer's taste is great"; // fonctionne ; "'" est un caractère invalide pour les noms de variable
echo "He drank some $beers"; // ne fonctionne pas ; 's' est un caractère valide pour les noms de variable
echo "He drank some ${beer}s"; // fonctionne
echo "He drank some {$beer}s"; // fonctionne
?>
De la même façon, l'index d'un tableau ou la propriété d'un objet peut être analysé. Avec les indices d'un tableau, les crochets (]) forment la fin de l'index. Les mêmes règles sont appliquées aux propriétés d'objet comme pour les simples variables.
<?php
// Ces exemples sont spécifiques à l'utilisation des tableaux dans des chaînes.
// Lorsqu'ils sont à l'extérieur d'une chaîne, quottez toujours les chaînes représentant
// les clés du tableau et n'utilisez pas d'{accolades}.
// Montre toutes les erreurs
error_reporting(E_ALL);
$fruits = array('strawberry' => 'red', 'banana' => 'yellow');
// Fonctionne, mais notez que cela fonctionne différemment à l'extérieur d'une chaîne
echo "A banana is $fruits[banana].";
// Fonctionne
echo "A banana is {$fruits['banana']}.";
// Fonctionne, mais PHP cherche une constante nommée banana en premier, tel que décrit ci-dessous
echo "A banana is {$fruits[banana]}.";
// Ne fonctionne pas, utilisez des accolades. Ceci produira une erreur d'analyse.
echo "A banana is $fruits['banana'].";
// Fonctionne
echo "A banana is " . $fruits['banana'] . ".";
// Fonctionne
echo "This square is $square->width meters broad.";
// Ne fonctionne pas. Pour une solution, reportez-vous à la syntaxe complexe.
echo "This square is $square->width00 centimeters broad.";
?>
Pour tout ce qui est encore plus complexe, vous devriez utiliser la syntaxe complexe.
Syntaxe complexe
Cette syntaxe est appelée complexe, non pas par qu'elle est complexe, mais parce qu'elle permet l'utilisation d'expressions complexes.
En fait, n'importe quelle valeur d'un espace de nom peut être inclue dans une chaîne de caractères avec cette syntaxe. Écrivez simple l'expression de la même façon qu'elle devrait l'être à l'extérieur de la chaîne et ensuite, entourez-là des caractères { et }. Sachant que le caractère { ne peut pas être échappé, cette syntaxe ne sera reconnue que lorsque le caractère $ suit immédiatement le caractère {. Utilisez {\$ pour afficher littéralement {$. Quelques exemples pour être clair :
<?php
// Montre toutes les erreurs
error_reporting(E_ALL);
$great = 'fantastic';
// Ne fonctionne pas, affiche : This is { fantastic}
echo "This is { $great}";
// Fonctionne, affiche : This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// Fonctionne
echo "This square is {$square->width}00 centimeters broad.";
// Fonctionne
echo "This works: {$arr[4][3]}";
// Ceci est faux pour la même raison pour laquelle $foo[bar] est faux à l'extérieur d'une chaîne.
// En d'autres termes, ceci fonctionnera, mais uniquement parceque PHP cherchera d'abord
// une constante nommée foo ; une erreur de niveau E_NOTICE (constante indéfinie) sera émise.
echo "This is wrong: {$arr[foo][3]}";
// Fonctionne. Lors de l'utilisation de tableaux multi-dimensionnels, utilisez toujours
// les accolades autour du tableaux lorsqu'il se trouve dans la chaîne
echo "This works: {$arr['foo'][3]}";
// Fonctionne.
echo "This works: " . $arr['foo'][3];
echo "This works too: {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
echo "This is the value of the var named by the return value of getName(): {${getName()}}";
echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
?>
Note: Les appels de fonctions et de méthodes dans {$} fonctionnent depuis PHP 5.
Accès et modification d'une chaîne, par caractère
On peut accéder et modifier les caractères d'une chaîne de caractères en spécifiant sa position (à partir de 0) en utilisant la même syntaxe que pour les tableaux. Il convient de voir une chaîne de caractères comme un tableau dans ce cas.
Note: On peut également accéder à une chaîne en utilisant des accolades, comme ceci : $str{42}. Cependant, cette syntaxe est obsolète depuis PHP 6. Utilisez les crochets à la place.
Exemple #5 Quelques exemples de chaînes
<?php
// Récupération du premier caractère d'une chaîne
$str = 'This is a test.';
$first = $str[0];
// Récupération du troisième caractère d'une chaîne
$third = $str[2];
// Récupération du dernier caractère d'une chaîne
$str = 'This is still a test.';
$last = $str[strlen($str)-1];
// Modification du dernier caractère d'une chaîne
$str = 'Look at the sea';
$str[strlen($str)-1] = 'e';
?>
Note: L'accès aux autres types de variables en utilisant [] ou {} retournera silencieusement NULL.
Fonctions et opérateurs utiles
Une chaîne de caractères peut être concaténée en utilisant l'opérateur '.' (point). Notez que l'opérateur '+' (addition) ne fonctionnera pas. Reportez-vous aux opérateurs de chaîne pour plus d'informations.
Il y a beaucoup de fonctions utiles pour la manipulation de chaîne de caractères.
Reportez-vous à la section sur les fonctions des chaînes de caractères pour plus de précisions et à la section sur les expressions rationnelles ou sur les expressions rationnelles compatibles Perl pour des recherches et des remplacements avancés.
Il y a également des fonctions pour les URL, et des fonctions pour chiffrer, déchiffrer les chaînes de caractères (mcrypt et mhash).
Et pour finir, reportez-vous aux fonctions "type de caractères".
Conversion en chaîne de caractères
Une valeur peut être convertie en une chaîne de caractères, en utilisant le mot clé (string) ou la fonction strval(). La conversion d'une chaîne de caractères est automatiquement effectuée dans le contexte d'une expression où une chaîne de caractères est nécessaire. Ceci survient lors de l'utilisation des fonctions echo() ou print() ou lorsqu'une variable est comparée à une chaîne. Les sections sur les types et sur le transtypage expliquent ce qui suit de manière plus détaillées. Reportez-vous également à la fonction settype().
La valeur booléenne TRUE est convertie en la chaîne "1". La valeur booléenne FALSE est convertie en "" (une chaîne vide). Ceci permet les conversions vers et depuis une chaîne et un booléen.
Un entier ou un nombre à virgule flottante est converti en une chaîne de caractères représentant le nombre de façon textuel (incluant la partie exponentielle pour les nombres à virgule flottante). Les nombres à virgule flottante peuvent être convertis en utilisant la notation exponentielle (4.1E+6).
Note: Le point décimal est définit dans la locale du script (catégorie LC_NUMERIC). Reportez-vous à la fonction setlocale().
Les tableaux sont toujours convertis en la chaîne "Array" ; ainsi, echo() et print() ne peuvent être utilisés pour afficher le contenu d'un tableau. Pour afficher un seul élément, utilisez un constructeur comme echo $arr['foo']. Voir ci-dessous pour des astuces permettant d'afficher le contenu complet.
Les objets en PHP 4 sont toujours convertis en la chaîne "Object". Pour afficher les valeurs des membres de l'objet (aux fins de déboguage, par exemple), lisez le paragraphes ci-dessous. Pour récupérer le nom de la classe de l'objet, utilisez la fonction get_class(). Depuis PHP 5, la méthode __toString est utilisée lorsqu'elle peut s'appliquer.
Les ressources sont toujours converties en la chaîne "Resource id #1", où 1 est le nombre unique assigné à la ressource par PHP au moment de l'exécution. Ne vous fiez pas à cette structure, il est possible qu'elle change. Pour récupérer le type de la ressource, utilisez la fonction get_resource_type().
NULL est toujours converti en une chaîne vide.
Au vue de tout cela, la conversion d'un tableau, d'un objet ou d'une ressource en une chaîne de caractères ne fournit aucune information utile sur la valeur contenue dans ce type. Reportez-vous aux fonctions print_r() et var_dump() pour plus d'informations sur le contenu de ces types.
La plupart des valeurs en PHP peuvent également être convertie en chaîne de caractères afin de les stocker. Cette méthode est appelée "linéarisation", et est effectuée par la fonction serialize(). Si le moteur PHP a été compilé avec le support WDDX, les valeurs PHP peuvent également être linéarisées en XML.
Conversion de chaînes en nombres
Lorsqu'une chaîne de caractères est évaluée dans un contexte numérique, la valeur et le type résultants sont déterminés comme suit.
La chaîne de caractères sera évaluée en nombre à virgule flottante si elle contient une des caractères suivants '.', 'e', ou 'E'. Sinon, elle sera évalué en un entier.
La valeur est fournie par la portion initiale de la chaîne de caractères. Si la chaîne de caractères commence par un caractère numérique valide, ce sera la valeur utilisée. Sinon, la valeur sera de 0 (zéro). Une valeur numérique valide est un signe optionnel, suivi par un ou plusieurs nombres (contenant, optionnellement, un point décimal), suivi par, éventuellement, un exponentiel. L'exponentiel est un 'e' ou 'E' suivi par un ou plusieurs nombres.
<?php
$foo = 1 + "10.5"; // $foo est un nombre à virgule flottante (11.5)
$foo = 1 + "-1.3e3"; // $foo est un nombre à virgule flottante (-1299)
$foo = 1 + "bob-1.3e3"; // $foo est un entier (1)
$foo = 1 + "bob3"; // $foo est un entier (1)
$foo = 1 + "10 Small Pigs"; // $foo est un entier (11)
$foo = 4 + "10.2 Little Piggies"; // $foo est un nombre à virgule flottante (14.2)
$foo = "10.0 pigs " + 1; // $foo est un nombre à virgule flottante (11)
$foo = "10.0 pigs " + 1.0; // $foo est un nombre à virgule flottante (11)
?>
Pour plus d'informations sur ces conversions, reportez-vous au manuel Unix de la fonction strtod(3).
Pour tester un exemple de cette section, copiez/collez l'exemple et insérez la ligne suivante pour voir ce qu'il se passe :
<?php
echo "Le type de \$foo==$foo; est " . gettype ($foo) . "<br />\n";
?>
Ne vous attendez pas à récupérer le code d'un caractère en le convertissant en entier, comme cela est possible en C. Utilisez la fonction ord() et la fonction chr() pour convertir les caractères en codes ASCII.
Les chaînes de caractères
16-Nov-2008 06:21
15-Oct-2008 11:33
An interesting finding about Heredoc "syntax error, unexpected $end".
I got this error because I did not use the php close tag "?>" and I had no code after the heredoc code.
foo1.php code gives "syntax error, unexpected $end".
But in foo2.php and foo3.php, when you add a php close tag or when you have some more code after heredoc it works fine.
Example Code:
foo1.php
1. <?php
2. $str = <<<EOD
3. Example of string
4. spanning multiple lines
5. using heredoc syntax.
6. EOD;
7.
foo2.php
1. <?php
2. $str = <<<EOD
3. Example of string
4. spanning multiple lines
5. using heredoc syntax.
6. EOD;
7.
8. echo $str;
9.
foo3.php
1. <?php
2. $str = <<<EOD
3. Example of string
4. spanning multiple lines
5. using heredoc syntax.
6. EOD;
7. ?>
30-Sep-2008 11:33
Simple function to create human-readably escaped double-quoted strings for use in source code or when debugging strings with newlines/tabs/etc.
<?php
function doubleQuote($str) {
$ret = '"';
for ($i = 0, $l = strlen($str); $i < $l; ++$i) {
$o = ord($str[$i]);
if ($o < 31 || $o > 126) {
switch ($o) {
case 9: $ret .= '\t'; break;
case 10: $ret .= '\n'; break;
case 11: $ret .= '\v'; break;
case 12: $ret .= '\f'; break;
case 13: $ret .= '\r'; break;
default: $ret .= '\x' . str_pad(dechex($o), 2, '0', STR_PAD_LEFT);
}
} else {
switch ($o) {
case 36: $ret .= '\$'; break;
case 34: $ret .= '\"'; break;
case 92: $ret .= '\\\\'; break;
default: $ret .= $str[$i];
}
}
}
return $ret . '"';
}
?>
11-Sep-2008 06:42
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:
<?php
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)
?>
02-Sep-2008 01:05
So you want to get the last character of a string using "String access and modification by character"? Well negative indexes are not allowed so $str[-1] will return an empty string.
<?php
//Tested using: PHP 5.2.5
$str = 'This is a test.';
$last = $str[-1]; //string(0) ""
$realLast = $str[strlen($str)-1]; //string(1) "."
$substr = substr($str,-1); //string(1) "."
echo '<pre>';
var_dump($last);
var_dump($realLast);
var_dump($substr);
06-Jun-2008 10:40
It's also valuable to note the following:
<?php
${date("M")} = "Worked";
echo ${date("M")};
?>
This is perfectly legal, anything inside the braces is executed first, the return value then becomes the variable name. Echoing the same variable variable using the function that created it results in the same return and therefore the same variable name is used in the echo statement. Have fun ;).
30-Apr-2008 10:46
<?php
$F = "F";
function F($s) { return $s; }
$filename = '<some code>';
echo "{$F(htmlspecialchars($filename))}";
?>
01-Apr-2008 05:21
This example of the heredoc has wrong output:
Code: This should print a capital 'A': \x41
Output should be: This should print a capital 'A': A
The example of the nowdoc has wrong code:
Code: This should not print a capital 'A': x41
That should be: This should not print a capital 'A': \x41
24-Mar-2008 04:58
For anyone who reads Evan K, please note that:
// a string to test, and show the before and after
$before = 'Quantity:\t500\nPrice:\t$5.25 each';
$after = expand_escape($before);
var_dump($before, $after);
Is identical to (note all I added was a backslash before $):
$before = "Quantity:\t500\nPrice:\t\$5.25 each";
var_dump($before);
So its definitely better to escape a dollar instead of all the overhead of his regex and evals and such, although clever completely unnecessary.
-Chris
28-Feb-2008 11:03
I encountered the odd situation of having a string containing unexpanded escape sequences that I wanted to expand, but also contained dollar signs that would be interpolated as variables. "$5.25\n", for example, where I want to convert \n to a newline, but don't want attempted interpolation of $5.
Some muddling through docs and many obscenties later, I produced the following, which expands escape sequences in an existing string with NO interpolation.
<?php
// where we do all our magic
function expand_escape($string) {
return preg_replace_callback(
'/\\\([nrtvf]|[0-7]{1,3}|[0-9A-Fa-f]{1,2})?/',
create_function(
'$matches',
'return ($matches[0] == "\\\\") ? "" : eval( sprintf(\'return "%s";\', $matches[0]) );'
),
$string
);
}
// a string to test, and show the before and after
$before = 'Quantity:\t500\nPrice:\t$5.25 each';
$after = expand_escape($before);
var_dump($before, $after);
/* Outputs:
string(34) "Quantity:\t500\nPrice:\t$5.25 each"
string(31) "Quantity: 500
Price: $5.25 each"
*/
?>
07-Feb-2008 08:31
I think there's not that much to string comparison as claiming date recognition:
It's simply comparing ordinal values of the characters from the {0} to the {strlen-1} one.
In this case
<?php
$a = '2007-11-06 15:17:48';
$b = '2007-11-05 15:17:48';
var_dump($a > $b);
?>
mArIo@luigi ~ $: php test.php
bool(true)
here all characters match till it reaches position 9 (the "day")
there, 6 has a bigger ord()inal value than 5
<?php
$a = 'January 25th, 2008 00:23:38';
$b = 'Janury 24th, 2008 00:23:37'; // ($a > $b) === false
?>
Here when we reach 'r' in "Janury" we see that "a" is "less" than "r" so the example would evaluate as ($a < $b) === true
Here:
<?php
$a = 'February 1st, 2008 00:23:38';
$b = 'January 25th, 2008 00:23:38';
?>
as expected the letter "F" comes before "J" as an ordinal character, so $a is less than $b
Even here:
<?php
var_dump('Z' > 'M'); //bool(true)
?>
it gets confirmed that the string comparison operators >, <, =>, =<, == just do a ordinal character comparison starting from position {0} to the first difference or the end of the string.
28-Jan-2008 06:25
@qriz at example dot com
Numerical comparisons, such as <, > are simply _NOT_ valid on strings. Thus, before a comparison can be made by a numerical comparison operator, the operands must be _casted_ to a numerical type (either float or int). What I was attempting to say in my previous post is that >, < are date-aware; the tests I included were examples, and not intended to represent the full scope of my comparison.
"Works correctly since there is a comparing between strings. The comparisson is done on the last number/letter (since thats the only thing that is difference in the string) and that is in this case: the 9 and 8."
What you say here is mere assumption; a few quick tests show that this is indeed not the case. If PHP indeed compares only the last character in the string, then the following assertion should be false:
test.php:
<?php
$a = '2007-11-06 15:17:48';
$b = '2007-11-05 15:17:48';
var_dump($a > $b);
?>
mArIo@luigi ~ $: php test.php
bool(true)
Further, consider the following choices for $a and $b, which, as expected, demonstrate that the <, > operators can indeed understand date formats:
<?php
$a = 'January 25th, 2008 00:23:37';
$b = 'January 24th, 2008 00:23:38'; // ($a > $b) === true, but 8
?>
If you remain unconvinced, consider what happens if I spell January incorrectly:
<?php
$a = 'January 25th, 2008 00:23:38';
$b = 'Janury 24th, 2008 00:23:37'; // ($a > $b) === false
?>
Looks like it can understand ISO 8601 date formats? (for more information, see http://en.wikipedia.org/wiki/ISO_8601)
Further investigation yields that this doesn't even work as it should:
<?php
$a = 'February 1st, 2008 00:23:38';
$b = 'January 25th, 2008 00:23:38';
var_dump($a > $b); //bool(false)
var_dump(strtotime($a)); //int(1201843418)
var_dump(strtotime($b)); //int(1201238618)
var_dump(strtotime($a) - strtotime($b)); //int(604800)
?>
Keeping $b constant and varying the month in $a shows that this comparison correctly interprets the date with the following months: January,March,May,June,July,September,October,November. Interestingly enough, these are all the months having the property that ord($a[0]) >= ord($b[0]).
<?php
var_dump('Z' > 'M'); //bool(true)
?>
Conclusion:
The <,> comparison operators definitely have functionality that is undocumented, including date awareness; however, this functionality may not always work as expected and should not be trusted for portability.
23-Jan-2008 04:38
this is the sql string that use the variable and and \' and function.It generate the correct result.
$sql1=<<<EOT
INSERT INTO hp_visitHistory ( col1,col2,col3)
VALUES ( NOW(), '{$col2}', '{$_SERVER['REQUEST_URI']}')
EOT;
echo $sql1;
13-Nov-2007 07:54
<?php
$a = '2007-11-05 15:17:49';
$b = '2007-11-05 15:17:48';
$bool = $a > $b;
var_dump($bool); //bool(true)
?>
works correctly since there is a comparing between strings. The comparisson is done on the last number/letter (since thats the only thing that is difference in the string) and that is in this case: the 9 and 8.
8 > 9 = true
if you want to compare the string as pure numbers then you must type cast it to numbers or type juggle it:
<?php
$a = '2007-11-05 15:17:49';
$b = '2007-11-05 15:17:48';
$bool1 = ($a + 0) > ($b + 0); // 2007 > 2007
$bool2 = (int) $a > (int) $b; // 2007 > 2007
$bool3 = intval($a) > intval($b); // 2007 > 2007
var_dump($bool1,$bool2,$bool3); //bool(false)
?>
06-Nov-2007 02:48
I have come across this several times, and as far as I can tell, the < and > operators have undocumented functionality when it comes to comparing strings. Consider the following script:
<?php
$a = '2007-11-05 15:17:49';
$b = '2007-11-05 15:17:48';
$bool = $a > $b;
var_dump($bool); //bool(true)
/**
* The manual tells us that $a and $b should be
* truncated at -, thus giving a floating-point value of 2007.
* But (2007 > 2007) === false...
*/
$a = (float)$a;
$b = (float)$b;
var_dump($a); //float(2007);
var_dump($b); //float(2007);
/**
* And the manual is right. So why does it correctly
* compare the dates (which should be treated
* as normal strings? Clearly some hidden functionality...
*/
26-Sep-2007 10:35
If you want to use a variable in an array index within a double quoted string you have to realize that when you put the curly braces around the array, everything inside the curly braces gets evaluated as if it were outside a string. Here are some examples:
<?php
$i = 0;
$myArray[Person0] = Bob;
$myArray[Person1] = George;
// prints Bob (the ++ is used to emphasize that the expression inside the {} is really being evaluated.)
echo "{$myArray['Person'.$i++]}<br>";
// these print George
echo "{$myArray['Person'.$i]}<br>";
echo "{$myArray["Person{$i}"]}<br>";
// These don't work
echo "{$myArray['Person$i']}<br>";
echo "{$myArray['Person'$i]}<br>";
// These both throw fatal errors
// echo "$myArray[Person$i]<br>";
//echo "$myArray[Person{$i}]<br>";
?>
07-Jul-2007 10:51
Heredocs can be used for more than just echoing or setting variables - use them whenever you want to include a string.
function header() {
return <<<EOT
<html>
<head>
<title>This is my heredoc</title>
</head>
<body>
EOT;
Also, note the strict syntax:
- No semicolon after initial EOT (think of the heredoc as a literal string arg - you wouldn't want a semicolon in front of it, would you?)
- BUT need semicolon after final EOT (the command is finished here)
- Final EOT is on the left margin - don't indent it!
04-Jul-2007 12:32
Function calls within double-quote variable interpolation work in PHP 5, but not quite as you'd expect. Basically the function has to be a variable function. I.e. a variable that holds the name of a function. So if you've got a function named 'x' that you want to call, you'll have to assign the function name to a variable. It's easiest to just assign it to a variable with the same name:
function x () { return 4; }
$x = 'x';
echo "x = {$x()}";
I'm not sure what the point of that is though, since it would be easier to do it this way:
function x () { return 4; }
$x = x();
echo "x = $x";
01-Jun-2007 06:31
Unlike bash, we can't do
echo "\a" #beep!
Of course, that would be rather meaningless for PHP/web, but it's useful for PHP-CLI. The solution is simple: echo "\x07"
26-Apr-2007 03:06
easy transparent solution for using constants in the heredoc format:
DEFINE('TEST','TEST STRING');
$const = get_defined_constants();
echo <<<END
{$const['TEST']}
END;
Result:
TEST STRING
24-Apr-2007 08:14
error control operator (@) with heredoc syntax:
the error control operator is pretty handy for supressing minimal errors or omissions. For example an email form that request some basic non mandatory information to your users. Some may complete the form, other may not. Lets say you don't want to tweak PHP for error levels and you just wish to create some basic template that will be emailed to the admin with the user information submitted. You manage to collect the user input in an array called $form:
<?php
// creating your mailer
$mailer = new SomeMailerLib();
$mailer->from = ' System <mail@yourwebsite.com>';
$mailer->to = 'admin@yourwebsite.com';
$mailer->subject = 'New user request';
// you put the error control operator before the heredoc operator to suppress notices and warnings about unset indices like this
$mailer->body = @<<<FORM
Firstname = {$form['firstname']}
Lastname = {$form['lastname']}
Email = {$form['email']}
Telephone = {$form['telephone']}
Address = {$form['address']}
FORM;
?>
01-Apr-2007 06:44
A simple benchmark to check differents about :
- simple and double quote concatenation and
- double quote and heredoc replacement
<?php
function test_simple_quote_concat()
{
$b = 'string';
$a = ' string'.$b.' string'.$b.' srting'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
$a .= ' string'.$b.' string'.$b.' string'.$b;
}
function test_double_quote_concat()
{
$b = "string";
$a = " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
$a .= " string".$b." string".$b." string".$b;
}
function test_double_quote_replace()
{
$b = "string";
$a = " string$b string$b string$b
string$b string$b string$b
string$b string$b string$b
string$b string$b string$b
string$b string$b string$b
string$b string$b string$b
string$b string$b string$b
string$b string$b string$b";
}
function test_eot_replace()
{
$b = <<<EOT
string
EOT; $a = <<<EOT
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
string{$b} string{$b} string{$b}
EOT;}
$iter = 2000;
for( $i=0; $i<$iter; $i++ )
test_simple_quote_concat();
for( $i=0; $i<$iter; $i++ )
test_double_quote_concat();
for( $i=0; $i<$iter; $i++ )
test_double_quote_replace();
for( $i=0; $i<$iter; $i++ )
test_eot_replace();
?>
I've use xdebug profiler to obtain the followed results:
test_simple_quote_concat : 173ms
test_double_quote_concat : 161ms
test_double_quote_replace : 147ms
test_eot_replace : 130ms
27-Feb-2007 10:16
As of (at least) PHP 5.2, you can no longer convert an object to a string unless it has a __toString method. Converting an object without this method now gives the error:
PHP Catchable fatal error: Object of class <classname> could not be converted to string in <file> on line <line>
Try this code to get the same results as before:
<?php
if (!is_object($value) || method_exists($value, '__toString')) {
$string = (string)$value;
} else {
$string = 'Object';
}
?>
21-Feb-2007 08:20
It may be obvious to some, but it's convenient to note that variables _will_ be expanded inside of single quotes if these occur inside of a double-quoted string. This can be handy in constructing exec calls with complex data to be passed to other programs. e.g.:
$foo = "green";
echo "the grass is $foo";
the grass is green
echo 'the grass is $foo';
the grass is $foo
echo "the grass is '$foo'";
the grass is 'green'
28-Mar-2006 11:58
You may use heredoc syntax to comment out large blocks of code, as follows:
<?php
<<<_EOC
// end-of-line comment will be masked... so will regular PHP:
echo ($test == 'foo' ? 'bar' : 'baz');
/* c-style comment will be masked, as will other heredocs (not using the same marker) */
echo <<<EOHTML
This is text you'll never see!
EOHTML;
function defintion($params) {
echo 'foo';
}
class definition extends nothing {
function definition($param) {
echo 'do nothing';
}
}
how about syntax errors?; = gone, I bet.
_EOC;?>
Useful for debugging when C-style just won't do. Also useful if you wish to embed Perl-like Plain Old Documentation; extraction between POD markers is left as an exercise for the reader.
Note there is a performance penalty for this method, as PHP must still parse and variable substitute the string.
30-Nov-2005 06:57
Use caution when you need white space at the end of a heredoc. Not only is the mandatory final newline before the terminating symbol stripped, but an immediately preceding newline or space character is also stripped.
For example, in the following, the final space character (indicated by \s -- that is, the "\s" is not literally in the text, but is only used to indicate the space character) is stripped:
$string = <<<EOT
this is a string with a terminating space\s
EOT;
In the following, there will only be a single newline at the end of the string, even though two are shown in the text:
$string = <<<EOT
this is a string that must be
followed by a single newline
EOT;
01-Nov-2005 06:05
Just some quick observations on variable interpolation:
Because PHP looks for {? to start a complex variable expression in a double-quoted string, you can call object methods, but not class methods or unbound functions.
This works:
<?php
class a {
function b() {
return "World";
}
}
$c = new a;
echo "Hello {$c->b()}.\n"
?>
While this does not:
<?php
function b() {
return "World";
}
echo "Hello {b()}\n";
?>
Also, it appears that you can almost without limitation perform other processing within the argument list, but not outside it. For example:
<?
$true = true;
define("HW", "Hello World");
echo "{$true && HW}";
?>
gives: Parse error: parse error, unexpected T_BOOLEAN_AND, expecting '}' in - on line 3
There may still be some way to kludge the syntax to allow constants and unbound function calls inside a double-quoted string, but it isn't readily apparent to me at the moment, and I'm not sure I'd prefer the workaround over breaking out of the string at this point.
27-Oct-2004 10:01
You can use the complex syntax to put the value of both object properties AND object methods inside a string. For example...
<?php
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 '$'.
<?php
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.
06-Aug-2004 10:03
A note on the heredoc stuff.
If you're editing with VI/VIM and possible other syntax highlighting editors, then using certain words is the way forward. if you use <<<HTML for example, then the text will be hightlighted for HTML!!
I just found this out and used sed to alter all EOF to HTML.
JAVASCRIPT also works, and possibly others. The only thing about <<<JAVASCRIPT is that you can't add the <script> tags.., so use HTML instead, which will correctly highlight all JavaScript too..
You can also use EOHTML, EOSQL, and EOJAVASCRIPT.
28-Apr-2004 05:49
watch out when comparing strings that are numbers. this example:
<?php
$x1 = '111111111111111111';
$x2 = '111111111111111112';
echo ($x1 == $x2) ? "true\n" : "false\n";
?>
will output "true", although the strings are different. With large integer-strings, it seems that PHP compares only the integer values, not the strings. Even strval() will not work here.
To be on the safe side, use:
$x1 === $x2
12-Apr-2004 01:53
Here is a possible gotcha related to oddness involved with accessing strings by character past the end of the string:
$string = 'a';
var_dump($string[2]); // string(0) ""
var_dump($string[7]); // string(0) ""
$string[7] === ''; // TRUE
It appears that anything past the end of the string gives an empty string.. However, when E_NOTICE is on, the above examples will throw the message:
Notice: Uninitialized string offset: N in FILE on line LINE
This message cannot be specifically masked with @$string[7], as is possible when $string itself is unset.
isset($string[7]); // FALSE
$string[7] === NULL; // FALSE
Even though it seems like a not-NULL value of type string, it is still considered unset.
20-Jan-2004 01:41
By the way, the example with the "\n" sequence will insert a new line in the html code, while the output will be decided by the HTML syntax. That's why, if you use
<?
echo "Hello \n World";
?>
the browser will receive the HTML code on 2 lines
but his output on the page will be shown on one line only.
To diplay on 2 lines simply use:
<?
echo "Hello <br>World";
?>
like in HTML.
12-Apr-2003 03:37
Note that in PHP versions 4.3.0 and 4.3.1, the following provides a bogus E_NOTICE (this is a known bug):
echo "$somearray['bar']";
This is accessing an array inside a string using a quoted key and no {braces}. Reading the documention shows all the correct ways to do this but the above will output nothing on most systems (most have E_NOTICE off) so users may be confused. In PHP 4.3.2, the above will again yield a parse error.
Regarding "String access by character":
Apparently if you edit a specific character in a string, causing the string to be non-continuous, blank spaces will be added in the empty spots.
echo '<pre>';
$str = '0123';
echo "$str\n";
$str[4] = '4';
