Talk:PHP/Variables

From Wikiversity
Jump to navigation Jump to search

Factual errors[edit source]

Variable conversion

The example and the text in this section is wrong. The second example will work without any errors (in any configuration, even if E_ALL is set). Instead, the result will be 4 * pi(), because the interpreter will cast the string to an numeric value, which means, that every char from the beginning of the string will be threated as a numeric value until an invalid character occurs.

Samples:

<?php var_dump((float) "1.11");  /* float(1.11) */ ?>
<?php var_dump((float) "1.11a"); /* float(1.11) */ ?>
<?php var_dump((float) "12vd");  /* float(12)   */ ?>

This behaviour is well documented, see PHP Manual.

Declaration

First, the C++ example is wrong. The string value should be double-quoted, because single-quotes have a different meaning in C. However, in C++ you would simply write something like string msg = "Stuff"; which is quite straightforward.

The second PHP example is also wrong, because the keyword "var" can only be used within PHP4 classes and not in global context. To "declare" a variable in PHP you have to assign a value, e.g. $a = 0. --The preceding unsigned comment was written by 84.44.172.234 (talk - contribs)

Thankyou for your suggestions. If you find something is wrong with the article, go ahead and fix it! Just click 'edit' at the top of the page. I'm not quite sure what you mean by the variable conversion issue - '42' * pi() works perfectly and multiplies pi by 42, not 4. --Draicone (talk) 00:24, 3 October 2006 (UTC)[reply]
I mean the second example, not the first one. It says, that '4string2' * pi() would lead to an error or a warning, but this is wrong. Instead, PHP will cast the string like i mentioned in the first comment.
Comparison C++ and PHP

The comparison of PHP and C++ variable declaration (in fact, what you show is declaration plus intialization) is completely useless and misleading: In the PHP example you simply show a variable initialization, while in C++ you show a function (why??), a declaration of a variable and an assignment statement. You can't compare these two examples. The equivalent C++ code (using integral types) would be like:

char* a = "Hello";

...which is as simple and straightforward as the PHP code.

Automagically[edit source]

Automagically? That's not a word! :-) --Jimmytharpe 18:07, 29 January 2007 (UTC)[reply]