Brackets for addition and casting in PHP?

bill

New member
1) I notice i cant do something like this in PHP,

echo "Length is " . $some_var + 4;

I have to add brackets like,

echo "Length is " . ($some_var + 4);

I suppose that is regular syntax maybe some languages you wouldn't need brakets there??

2) As for casting there can't really be any in PHP as there are no variables types?

For example if i have

$char_var = "5";

$add_var = $char_var +7;

I haven't even tried this i should have first, but anyway,

like in other langaues you would have to cast the char "5" into say an int, like

$add_var = int($char_var) +7;

What are the basic rules?
 
PHP is not very strict in variable types. This is one advantage of PHP against other language. Most of the time, you can use variable w/o specifying its data type, you can use it depending on the context you are using the variable.
 
Back
Top