who can tell me what it's meaning of the following code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
addaddc
Forum Newbie
Posts: 1
Joined: Thu Apr 09, 2009 8:15 am

who can tell me what it's meaning of the following code?

Post by addaddc »

<?php $act && $act = 'view'; ?>


$act is a string.

Thank you.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: who can tell me what it's meaning of the following code?

Post by jayshields »

Without testing I assume all it does is assign 'view' to $act.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: who can tell me what it's meaning of the following code?

Post by Mark Baker »

addaddc wrote:<?php $act && $act = 'view'; ?>
What do you get if you var_dump $act immediately afterwards?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: who can tell me what it's meaning of the following code?

Post by jayshields »

I thought I'd test it as I thought it could actually be doing something useful.

I think it's intended as a quick and dirty version of

Code: Select all

if(isset($act)) $act = 'view';
This script

Code: Select all

<?php
$act && $act = 'view';
var_dump($act);
?>
produces

Code: Select all

Notice: Undefined variable: act in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\act.php on line 2
Notice: Undefined variable: act in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\act.php on line 3
NULL
Whereas...

This script

Code: Select all

<?php
$act = 'x';
$act && $act = 'view';
var_dump($act);
?>
produces

Code: Select all

string(4) "view"
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: who can tell me what it's meaning of the following code?

Post by Benjamin »

It would appear to me to be the source of a bug.

Most likely it should be:

Code: Select all

 
<?php $act && $act == 'view'; ?>
 
This would test that $act is not empty, 0 or false and then verify that it is equal to the string 'view'.
Post Reply