By first time it seems to be so simple, with no error, but just check it:
1 $c1 = "9515161516516516516156";
2 $lenght = strlen($c1);
3 $delimiter = 4;
4 $с1 = $lenght / $delimiter;
5 echo $c1;
returns
Parse error: syntax error, unexpected '?', expecting
T_VARIABLE or '$' in /home/public_html/test.php on line 4
If anyone understands, please answer it.
The c in $c1 = ... on line four is not the ASCII letter c. It is the Unicode character U+0441 CYRILLIC SMALL LETTER ES which looks exactly like an ordinary c.
Delete it from your source file and type in an ordinary c instead.
Related
I would like to convert Unicode codepoint to character. Here is what I have tried:
$point = dechex(127468); // 1f1ec
echo "\u{1f1ec}"; // this works
echo "\u{$point}"; // this outputs '\u1f1ec'
echo "\u{{$point}}"; // Parse error: Invalid UTF-8 codepoint escape sequence
echo "\u\{{$point}\}"; // outputs \u\{1f1ec\}
echo "\u{". $point ."}"; // Parse error; same as above
You don't need to convert integer to hexadecimal string, instead use IntlChar::chr:
echo IntlChar::chr(127468);
Directly from docs of IntlChar::chr:
Return Unicode character by code point value
A similar problem occurs when you want to get a floating point number, say, 12e-4, concatenating pieces. The parsing is done too early in the compiler to allow it. You probably can, however, use eval() to do so. Yuck.
Actually find the solution after several hours:
$unicode = '1F605'; //😅
$uni = '{' . $unicode; // First bracket needs to be separated, otherwise you get '\u1F605'
$str = "\u$uni}";
eval("\$str = \"$str\";"); // Turns unicode into RegEx and store it as $str
echo $str;
Thanks #Rick James for the idea with the eval() function
PHP 7+ solution snippet:
function charFromCodePoint($codepoint) {
eval('$ch = "\u{'.dechex($codepoint).'}";');
return $ch;
}
Notice, that PHP5 doesn't support the "\u{}" syntax.
I've seen in this answer the code
$tmpNode = parent::addChild($name,null,$namespace);
$tmpNode->{0} = $value;
I'm curious what the ->{0} actually does? Which PHP language construct is this? Does it reference the first property of $tmpNode without using its name?
Update:
I've seen the answers given so far, but I was looking for a reference into the PHP language manual that explains this use of curly braces. When I search in the PHP Manual for curly the only hit is to the page about strings where curly's are only explained in the context of variables and complex expressions. It wasn't clear to me that the language allows curly's around literals.
Curly brackets {} in php are also used to parse complex codes. Take this for example:
$t = 0;
$$t = 5;
echo ${0}; //outputs 5
or this:
${0} = 65;
echo ${0}; //outputs 65
but if you were to try this:
$0 = 65;
echo $0;
you would get:
Parse error: syntax error, unexpected '0' (T_LNUMBER), expecting variable (T_VARIABLE) or '$'
It is the same with object properties:
$obj = new stdClass();
$obj->{1} = "Hello world";
echo $obj->{1}; //outputs "Hello world"
Complex (curly) syntax
Today i was working with some codes when I met this error to make it simplify I have made an simple code which returns this error:
$i=1;
echo $i*5."<br/>";
Error
syntax error, unexpected '"<br/>"' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
Here I am trying to multiply an integer variable with an integer value and then add some string afterwords.
the solution I found to escape this error is to simply replace $i*5 by 5*$i but it my question is why does this happens.In my scope ov view there is no syntax error but if there is any please let me know.
The reason for the error is the . after 5 which makes compiler confused whether 5 is an integer or an floating value i.e it expects some digits after . but it gets "<br/>"
You can add an space after the digit so that the compiler gets to know that number is over like this :
$i=1;
echo $i*5 ."<br/>";
The correct syntax is either
echo $i*5, "<br/>";
// You can echo more than one expression, separating them with comma.
or
echo $i*5 . "<br/>";
// Notice the space.
// 5. is interpreted as ( float ) 5
I have the following string coming from a database: Let's Get Functional
If I run this through strlen it returns 25 characters instead of the expected 20. A var dump shows that the string looks like the above (no html references etc).
If I remove the single quote strlen returns 19 characters.
Obviously the quote is returning 5 characters instead of 1 - why? How can I stop this?
Thanks!
The ASCII entity name is ' for ', that equals to 5 chars : ASCII Table
The problem seems to be related to the fact that your ' is evaluated, not just readed.
Try to get your strings like that :
$myString = 'Hello World!';
Not like this :
$myString = "Hello World!";
Which reads and evaluates all your string's content instead of just reading, and so interpreting your special chars as their ASCII code.
PHP Manual says : If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters
I think your strlen() function is called with parameters containing ", so it gives the evaluated strlen(), not the readed.
try this :
$countChars = strlen(utf8_decode($myString));
utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.
Take a look at this for more informations about differences between simple and double quotes.
As #deformhead already explained, it seems that your apostrophe has been converted to the HTML ' string. My guess would be that between getting the string out of the database and calling strlen() on it you call htmlentities() somewhere in-between.
You can also check how many characters you get from the database in your select query with CHAR_LENGTH() (MySQL).
Another issue you might consider is that strlen() does not work well for multibyte characters so if you'll be working with non-ASCII characters then you'd better use mb_strlen() with the correct encoding. This case however would not explain the difference of 5 characters in your result (strlen() counts the bytes and not characters in a string).
Hope that helps.
It can not be.
<?php
$str = "Let's Get Functional";
echo strlen($str), "\n"; // 20
Look at code output here.
how to debug?
print the ASCII code of each char:
$str = "Let's Get Functional";
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
echo "$i\t", ord($str[$i]), "\n";
}
this is the result:
0 L 76
1 e 101
2 t 116
3 ' 39
4 s 115
5 32
6 G 71
7 e 101
8 t 116
9 32
10 F 70
11 u 117
12 n 110
13 c 99
14 t 116
15 i 105
16 o 111
17 n 110
18 a 97
19 l 108
<?php
$string = "Let's Get Functional";
echo strlen($string);
?>
This code returns 20 characters.
I have had same problem as you , may be this will help someone.
The single quote was converted to "& #39;" which was giving me incorrect result.
simply replacing the string with single quote have solved my problem.
$string = "Let's Get Functional"; //string from POST or Database
echo strlen($string); //25
$string = str_replace("'", "'",$string);
echo strlen($string); //20
I'm having a problem with some code that used to work in PHP 4.X and is not working in PHP 5.2.4
First of all, there is a small example of a code similar to the one is causing the problem. Unfortunately, I haven't been able to reproduce the problem with a small example.
<?php
class Example{
public function showExample()
{
$ind = 1;
$m = "method";
$str2 = "{call method}";
$str2 = str_replace("{call $m}" , "<?php print( \$pre$ind ); ?>", $str2);
echo $str2 . "\n";
}
}
$e = new Example();
$e -> showExample();
?>
What this code is doing is building a string with some php code to execute later on. In particular, the code generated will print the value of a variable named "$pre" + a variable number. In this case, the output is the following:
<?php print( $pre1 ); ?>
Everything runs fine with this code. The problem is when I use it in the context of a much bigger class, that is part of a framework I've been using for a long time. I cannot paste here the whole source of the class, but the problematic lines are the following (I simplified them a little bit to remove the str_replace, but the error still appears):
$myVar = "value";
$myVar2 = 2;
$str2 = "<?php print( \$myVar$myVar2 ); ?>";
When I load the file, I get the following two messages:
PHP Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /Users/giove/Sites/mundial/htmltemplate.php on line 252
PHP Parse error: syntax error, unexpected T_VARIABLE in /Users/giove/Sites/mundial/htmltemplate.php on line 252
I can fix the warning by removing the '\', but that changes the semantics of the code, so it's not a real possibility.
Now, the weirdest part is I can remove both errors by breaking or removing the sequence "
This seems to be a difference in versions, but I haven't been able to find any mention to it on the change logs.
Now I've got a working solution from Cryo: split the string
"<?php"
to prevent its 'evalution' (I'm not sure if that's really an evaluation).
Nevertheless, I still would like to know the reason for this weird behavior.
Cryo: thanks for your help, I'll mark the question as answered in a couple of days.
My guess is that PHP is catching on the re-opening of the php tag <?php, try splitting just that:
$str2 = "<?" . "php print( \$myVar$myVar2 ); ?>";
Or use single quotes and concatenation:
$str2 = '<?php print( $myVar' . $myVar2 . ' ); ?>';
Cryo is on the right track, though I think the actual issue is that PHP evaluates variables within double-quoted strings. However, the slash should prevent the variable from being evaluated. So:
$a = "somestring"
$b = "\$a" // -> \$a
$c = '\$a' // -> \$a
I think your string is getting evaluated in an odd way such that the \ + $myVar is evaluated in a strange way.