PHP 5.4 Integer Problem

Hi I am a beginner in PHP an currently learning Integer.
In my IDE I am not getting a report of a problem, but something seems to be wrong, because I get this when I try to run the program. Parse error: syntax error, unexpected T_STRING in /data/multiserv/users/733259/projects/1775490/www/newfile.php on line 2
[php]<?php
$right_write = 0b10;
$right_read = 0b01;

$user_rights = 0b01;
//Diese Operation ergibt 01 und wird zu “true” konvertiert
if ( true == (bool) ($user_rights & $right_read))
{
echo “Reading Allowed”;
}

if (true == (bool) ($user_rights & $right_write))
{
echo “Writing allowed”;
}
[/php]

I hope you can help me and I am very sorry for my bad english and php knowledge :slight_smile:
I started reading the book yesterday^^
Thanks for helping me out.

If the value of a variable has letters it should be a string.

[php]$right_write = “0b10”;
$right_read = “0b01”; [/php]

A freind told me that it could be that PHP thinks that the TAB i wrote in line 3 is seen by the compiler as a wrong not supported symbol.
[php]$right_write = 0b10;
$right_read = 0b01; [/php]

your friend is wrong. Spring is correct, if you have any non-numbers in there, its considered a string. You can have as many tabs in there as you want, 2000 if you want and php would care less. Programmers inset all their code as a way to keep things organized and code seperated, like
[php]
while($s = mysql_fetch_array($result)) {
some code tabbed;
another line = tabbed;
line = tabbed;
}[/php]
None of those lines would throw an error message.

Other langeages might not like them, but i haven’t seen any.

papalakaka,

Are you certain that you are on a 5.4+ server? Your code executes fine for me as presented without any errors.

You can check your php version using[php]echo phpversion();[/php]
You DO NOT want to use quotes around binary, octal, or hexidecimal notation values as your values will be treated as strings instead of integers as you intended.

You might try replacing your binary notation variables with octal or hexidecimal notation instead and see if it is still throwing an error. For example:[PHP]$right_write = 0x02;
$right_read= 0x01;[/PHP]Note that I removed the tab; however, it shouldn’t matter and you could leave it in for testing if you prefer.

How about this?

[php]<?php
$right_write = “0b10”;
$right_read = “0b01”;

$user_rights = “0b01”;
//Diese Operation ergibt 01 und wird zu “true” konvertiert
if ($user_rights == $right_read) {
echo “Reading Allowed”;
} else if ($user_rights == $right_write) {
echo “Writing allowed”;
}
?>[/php]

Andrew,

Your suggestion would work fine for the code that was presented, but I think the op’s code was really meant for learning to use the new(ish) binary notation feature of PHP.

The problem with using quotes is that instead of being interpreted as a binary number (0b10 = 2), it would be treated as a string.

Consider this:[php]$num = “0x0a”;
echo $num++;[/php]
The output will be0x0a

Now try:[php]$num = 0x0a;
echo $num++;[/php]
The output will be10

You will note that I used hexidecimal instead of binary, as it is supported in far more versions of php. If I am misunderstanding the op’s intent, your code should definitely work for him.

Ah… I have not played with 5.4 yet… That would explain why it did not make sense. However, if the notation is correct then my code just needs the ""s removed and it will still work.

Sponsor our Newsletter | Privacy Policy | Terms of Service