CanMan2004 Posted January 19, 2007 Share Posted January 19, 2007 Hi allI have an IF statement, but how do you write the IF statement so does"if it contains anything", for example[code]if ($_GET['action'] == '%')[/code]But I cant use that, as '%' doesnt seem to work.Bacially, my url can look like any of the followingthankyou.php?action=addthankyou.php?action=editthankyou.php?action=deletethankyou.php?action=viewSo I want to say IF $_GET['action'] == ANYTHINGCan this be done?ThanksMark Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/ Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 if($_GET['action']){} Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164451 Share on other sites More sharing options...
JayBachatero Posted January 19, 2007 Share Posted January 19, 2007 I would use this since it won't return an error id action is not set.[code]if (isset($_GET['action'])){ ...}[/code] Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164454 Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 What I wrote won't return an error either, and it's shorter :-P Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164457 Share on other sites More sharing options...
boo_lolly Posted January 19, 2007 Share Posted January 19, 2007 isset() is better. if another coder is working on your script after you, it will make more sense to him/her. Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164459 Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 Why? I've never even seen isset until I saw people post it here, and I've worked for several companies with other people's code. Part of being a "coder" is knowing how to use the manual and look up something you don't know. But leaving it out is pretty obvious.If($x) means if there is $x... if(isset($x)) is throwing in unnecessary functions IMO. Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164462 Share on other sites More sharing options...
CanMan2004 Posted January 19, 2007 Author Share Posted January 19, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164464 Share on other sites More sharing options...
boo_lolly Posted January 19, 2007 Share Posted January 19, 2007 [quote author=jesirose link=topic=123144.msg508606#msg508606 date=1169219667]Why? I've never even seen isset until I saw people post it here, and I've worked for several companies with other people's code. Part of being a "coder" is knowing how to use the manual and look up something you don't know. But leaving it out is pretty obvious.If($x) means if there is $x... if(isset($x)) is throwing in unnecessary functions IMO. [/quote]i'm aware of that. i've worked for (and am working now) for companies where others are going to be manipulating my code after me. it is best to adhere to their skill level (whatever it may be). so if($i) is the same as if(isset($i)), of course. but which one makes more sense when you read it? Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164467 Share on other sites More sharing options...
JayBachatero Posted January 19, 2007 Share Posted January 19, 2007 But if you are doing [code=php:0]if ($i)[/code] and you are not getting any errors (a) you defined $i before the if statement (b) error_reporting is off. Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164471 Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 "but which one makes more sense when you read it?" - To me, it's if($x) - I guess it's personal preference. I have error reporting, but not notices, so I've never seen any errors with it, in years of working that way. It really doesn't matter, use isset if you prefer ;) Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164476 Share on other sites More sharing options...
Nameless12 Posted January 19, 2007 Share Posted January 19, 2007 try php5.20 I think they modified a few of the error handling things and if you try php5.20 im sure you will start getting notices where you never used to.Also think of it this way $some_var = 'some value';if ($some_var) {}the above code goes if $some_var, it then gets the value of some var and checks if it is true. It is quicker to check if a value is set rather than retrieve it and check if the value is true. And it is also faster than trying to get a key that does not exist. I never really thought about it that much until i started getting notices in php5.20 but now it makes perfect sense. Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164497 Share on other sites More sharing options...
redbullmarky Posted January 19, 2007 Share Posted January 19, 2007 [quote author=jesirose link=topic=123144.msg508601#msg508601 date=1169219253]What I wrote won't return an error either, and it's shorter :-P[/quote]depending on the error_reporting level, it will return a 'notice - undefined index' which - although wont stop the script, it'll look a bit horrible with an message at the top of the screen.so whilst notices can safely be ignored/hidden without any detrimental effects, it's good practice to deal with them too.[code]<?phpif (isset($_GET['action']) && $_GET['action']){ ... rest of code}?>[/code]tis like a HTML/CSS validation warning. You dont HAVE to deal with it, and all things may work still the way intended, but it's always good to tidy up :) Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164503 Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 redbull - yeah after some AIM discussion we determined it's a notice, and I have notices turned off (amazingly on every system I've ever used!)I'm stubborn. :) Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164507 Share on other sites More sharing options...
boo_lolly Posted January 19, 2007 Share Posted January 19, 2007 [quote author=jesirose link=topic=123144.msg508651#msg508651 date=1169223199]redbull - yeah after some AIM discussion we determined it's a notice, and I have notices turned off (amazingly on every system I've ever used!)I'm stubborn. :)[/quote]i'm sure you know it is ALWAYS a good idea to have error_reporting(E_ALL)... i don't know why you wouldn't, ever. Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164523 Share on other sites More sharing options...
Jessica Posted January 19, 2007 Share Posted January 19, 2007 Because the systems I am developing for are normally set to a lower degree of error reporting. Sometimes in a business environment, you don't get to change everything you want to - several times I've had to work on systems with Register Globals on. Did I want it that way? No, but it wasn't up to me.I usually turn error_reporting off once I'm DONE developing, and log errors. When I develop, I turn them back on. I just don't see notices as errors. If they change that in PHP6, I will adapt to it, but still those systems will be using PHP 4 so really, it won't impact me much. As long as I am seeing my ERRORS, I feel it's fine. Chillax. Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164526 Share on other sites More sharing options...
redbullmarky Posted January 19, 2007 Share Posted January 19, 2007 [quote author=jesirose link=topic=123144.msg508651#msg508651 date=1169223199]redbull - yeah after some AIM discussion we determined it's a notice, and I have notices turned off (amazingly on every system I've ever used!)I'm stubborn. :)[/quote]TBH, i was exactly the same until i started getting into frameworks recently and studying their code, and when i saw an 'isset' as WELL as a non-empty check or whatever, I looked into it a bit more. As far as I know and have read, until fairly recently, error reporting was set by default to E_ALL ~ E_NOTICE or something like that (and most hosts I used, including my current one, were the same), which basically had all the errors and warnings showing but not the notices. I used to just use an installation out of the box, and then (as you do) just turn off display_errors when the site goes live.In most cases, it's fine - but obviously the problem would become quite apparent if you had to shift an entire site over onto a server that (for example) has error_reporting set to the max and no way of changing it. So I actually agree in some ways that it's unnecessarily extra code and an extra function call, but the difference doesnt even register enough on the how-long-to-generate-this-page-o-meter to worry much about.Like you say though jesi - it's so uncommon for mosts hosts to have notices showing, or to have no way of knocking them off, that it [i]can[/i] come down to personal preference and shortcuts. For example, I use short tags (<? ?> and <?= ?>) and alternative PHP syntax when doing my templates, which a) is sometimes stated as bad practice and b) can also be turned off on some installations.Oh well ;Dcheers Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164543 Share on other sites More sharing options...
Nameless12 Posted January 19, 2007 Share Posted January 19, 2007 About how you use short tags do you know why php5.20 turned them off by default? Link to comment https://forums.phpfreaks.com/topic/34886-if-statement/#findComment-164681 Share on other sites More sharing options...
Recommended Posts