tags:

views:

97

answers:

5
$value = '\\40';
file_put_contents('o.txt',$value);
file_put_contents('o2.txt',var_export($value,true));


D:\test>php str.php
D:\test>cat o.txt
\40
D:\test>cat o2.txt
'\\40'
A: 

Because var_export outputs or returns a parsable string representation of a variable

http://de.php.net/manual/en/function.var-export.php

Your code works perfectly. ;) Does just what it is expected to.

Lennart
+1  A: 

var_export($value, true) returns the string declaration value '\\40' while just $value returns the interpreted value of that declaration, thus \40.

Gumbo
A: 

The value of $value is '\40' (that is backslash, four, zero). When you just type it, this is what you get.

var_export gives you valid php code, in which you need to backslash the backslash, as you did yourself in the first line of code.

Does that make sense?

yhager
A: 

Because the output from var_export is encoded so you could put the result into a PHP file and include it or pass it to a call to eval. $value = \40 as o.txt shows. But if you were to say $x = eval(file_get_contents('o2.txt')); x would also = \40.

jmucchiello
A: 

$value = '\40';

really means '\40', the first "\" escapes the second.

var_export -- Outputs or returns a parsable string representation of a variable

var_export adds a "\" so it is escaped and parsable:

'\\40'
KM