jvrothjr Posted February 12, 2013 Share Posted February 12, 2013 I am working on updating code to work in 5.4 php and am having some issues with IF statements and reaction IF(empty($var) = check if 0,empty or not set IF(isset($var)) = check if set (this has issues in 5.4 I am replacing but what is the best replacment) IF(!$var) = check not set IF($var) = Check if set IF($var1 <> $var2) = Replace with IF($var1 != $var2) Just trying to understand. I did not write all the code but am trying to standardize on a common format Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 A. this has nothing to do with regex. B. You didn't ask a question. C. Your explanations of the code are incorrect. Are you asking what the code does? The manual explains it, as well as how they've changed from version to version. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted February 12, 2013 Author Share Posted February 12, 2013 I have used this forum for years as ref but not any more. Please delete this post and remove My ID from this Forum I have got nothing but neg with the last few posts I will take my questions and skills else where. Use to be staff would just move the post but now they warn and complain. and there is a question in this post if you read it (this has issues in 5.4 I am replacing but what is the best replacment) Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted February 12, 2013 Author Share Posted February 12, 2013 C. Your explanations of the code are incorrect. Are you asking what the code does? The manual explains it, as well as how they've changed from version to version. if so what is the correct definition. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 You've been a member of this forum for TEN YEARS and haven't learned to post in the right forum, and you want us to keep doing it for you without complaining? In other words, you've been a member here LONGER THAN I HAVE and you haven't figured out the difference between the PHP help forum and the Regex help forum, and you're upset that someone told you so? PS: Questions end in question marks. If that is your only question, don't hide it in the middle of a list of code. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 12, 2013 Share Posted February 12, 2013 The problem is your question only states that IF(isset($var)) is having problems and you want to know what is a better replacement. You need to state exactly what you are trying to verify. isset() has been around since PHP4 and has had only 1 update 5.4.0 Checking non-numeric offsets of strings now returns FALSE Which is really an edge case scenario in my opinion and wouldn't cause it to start behaving differently for you. Most likely, some logic has changed such that the condition is not returning the same results as before because the preconditions have changed. So, you need to determent what it is you want to verify. Are you wanting to know if the variable is set, if it is empty, if it is FALSE, or what? FYI: I see a lot of people do something like this $var = $_POST['foo']; if(isset($var)) { //Do something } which will always result to true because $var was set immediately before the condition check. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 13, 2013 Share Posted February 13, 2013 FYI: I see a lot of people do something like this $var = $_POST['foo']; if(isset($var)) { //Do something } which will always result to true because $var was set immediately before the condition check. $var won't be set if the POST variable isn't set. In other words, if someone goes straight to the form processing page without even looking at the form, the POST variables won't be set. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 14, 2013 Share Posted February 14, 2013 $var won't be set if the POST variable isn't set. In other words, if someone goes straight to the form processing page without even looking at the form, the POST variables won't be set. Yes and No. You are correct that isset() will return false in that condition. But, it is not because $var is not set - it is. In that scenario $var is set to a value of NULL. I forgot that isset() checks to Determine if a variable is set and is not NULL. You can verify that $var is set by turning on error reporting to E_ALL and doing an echo $var; which will not report an Undefined Variable warning. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted February 15, 2013 Author Share Posted February 15, 2013 Thanks Psycho at least some are willing to help in place of complaining. I have a page that uses all these forms and create different issues on the same page and just trying to get it standardized and working right on the newest php version. Because it is not acting the same as it was on php 5.2.1 as 5.4.3. I have added this string to see if post is null if not then set the $var which helps stop setting value on first page load if the value is set within the page. if ($_POST['foo']) {$var = $_POST['foo'];} if(isset($var)) { //Do something } Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 15, 2013 Share Posted February 15, 2013 (edited) Have you tried turning on all errors for PHP? <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> If $_POST['foo'] isn't set a notice will be shown about foo being undefined. To avoid that, the code could be modified to something like: <?php if(isset($_POST['foo'])) { $var = $_POST['foo']; //Do something } ?> Also, have you considered using $_POST['foo'] as the variable instead of $var? Edited February 15, 2013 by cyberRobot Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2013 Share Posted February 15, 2013 if ($_POST['foo']) {$var = $_POST['foo'];} if(isset($var)) { //Do something } That is something I would never do. I never use something such as if($var) { //do something if $var is an 'unknown' quantity - i.e. I cannot be sure if it exists or what value it may contain. This is along the lines of what I would typically do: $foo = (isset($_POST['foo'])) ? trim($_POST['foo']) : ''; if(!empty($foo)) { //Do something } But, that would not work if a user can submit an empty value and I want to perform the "Do something" process on it as well. In that case I might use $foo = (isset($_POST['foo'])) ? trim($_POST['foo']) : false; if($foo !== false) { //Do something } It all depends on understanding what the possible inputs can be (or not be) and building the functionality accordingly. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted February 15, 2013 Author Share Posted February 15, 2013 $foo = (isset($_POST['foo'])) ? trim($_POST['foo']) : false; if($foo !== false) { //Do something } This is along the lines i think he intended for it to be used The function was only to execute if the variable was set. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.