Jump to content

if () issues


jvrothjr

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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 by cyberRobot
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.