Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [php] equality or assignment? (https://thetfp.com/tfp/tilted-technology/62603-php-equality-assignment.html)

trache 07-15-2004 11:35 AM

[php] equality or assignment?
 
I have seen the following code:

if ($blah = do_this()) {

[...]

};

While I'm quite a competent programmer, I still have an icky feeling when it comes to PHP only because the documentation on the website blows. and no one except the PHP programmers know what they're doing.

So what is this statement performing?

Is it assigning do_this() to $blah and returning true (all the time) or testing if $blah equals do_this() (which I don't think because usually == is equality and = is assignment)?

*shrugs*

Rawb 07-15-2004 11:38 AM

It is setting $blah to the output of do_this(), and the if statement is also getting the output of do_this() to evaluate to be true or false (basically, if do_this() returns false, the if statement won't fire)

cthulu23 07-15-2004 07:24 PM

PHP is very C like in it's syntax, so = is the assignment operator and == is an equivalence test. === tests for type equivalence as well as value equivalence.

SinisterMotives 07-16-2004 07:03 AM

That construct is most frequently used to test whether a resource handle can be acquired so that your script can determine whether it can perform operations on the handle. For example, the following code would test to see if $fp is a valid file handle:

PHP Code:

if($fp = @fopen('some_file.dat','r')) {
 
fread($fp,filesize('some_file.dat'));
 
fclose($fp);


Prefixing the "@" symbol to a function name suppresses any error message that would be printed if the attempt to acquire a handle failed.


All times are GMT -8. The time now is 08:31 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37