/    Sign up×
Community /Pin to ProfileBookmark

Both Conditions in the IF & ELSE Get Echoed! BIGGEST MYSTERY IN PROGRAMMING!

Folks,

Look at this script below! Both conditions from the IF & ELSE get echoed!
If “Line 53!” gets echoed (IF), then “Line 58!” (ELSE) should not get echoed and vice versa.
Makes sense. Right ?
Look at the attached img for what is getting echoed!
Fireup your Lamp/Xampp/Wamp and feed this code and check it out on your LocalHost.
What a mystery! Right ?

PROBLEM IN THIS SNIPPET …

[code]
if(!isset($_POST[“$value_2”]) && empty($_POST[“$value_2”]))
{echo “Line 253!<br>”;//echo $_POST[‘form_question_label_required’];
//die(“Fill-in All required Form Fields that! Fields with asterisks * are required to be filled-in!”);
}
else
{
echo “$value_2<br>”; echo “Line 258!<br>”;
}
[/code]

FULL CODE

[code]
<?php

?>

<!DOCTYPE HTML”>
<html>

<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1″>
</head>
<body>

<?php
/*
session_start();

//$_session = ‘search’;
//echo session_id();
//echo $_session;

if(isset($_GET[‘session_type’]))
{
$session=$_GET[‘session’];echo $_session[‘session’];
}
else
{
die(“Invalid Session1!”);echo $_session[‘session’];
}
*/

/*
echo session_id();

session_start();
if(isset($_GET[‘session_type’]))
{
$_GET[‘session_type’];
}
else
{
die(“Invalid Session2”);
}
*/

session_start();

//if($_SESSION[‘session_step’] != ‘end’);
{
$_SESSION[‘session_step’] = ‘start’;
echo $_SESSION[‘session_step’]; echo “<br>”;
echo session_status(); echo “<br>”;
echo “Line 52<br>”;

if(isset($_GET[‘session_type’]) && !empty($_GET[‘session_type’]))
{
$_SESSION[‘session_type’] = $_GET[‘session_type’];
//echo session_id();
if(!function_exists($_SESSION[‘session_type’]))
{
die(“Invalid Session”);
}
else
{
$_SESSION[‘session_type’]();
echo “Line 63<br>”;
}
}
else
{
die(“Invalid Session”);
//echo session_id(); DELETE
}

function submit_personal_details()
{
$session_type = $_SESSION[‘session_type’];

$form_questions_labels[]=’Title’;
$form_questions_labels[]=’First Name’;
$form_questions_labels[]=’Middle Name’;
$form_questions_labels[]=’Surname’;
$form_questions_labels[]=’Gender’;
$form_questions_labels[]=’Age_Range’;
$form_questions_labels[]=’Marital Status’;
$form_questions_labels[]=’Working Status’;
$form_questions_labels[]=’Tos’;

$form_questions_labels_required[]=’Title’;
$form_questions_labels_required[]=’First Name’;
$form_questions_labels_required[]=’Surname’;
$form_questions_labels_required[]=’Gender’;
//$form_questions_labels_required[]=’Age_Range’;
$form_questions_labels_required[]=’Marital Status’;
$form_questions_labels_required[]=’Working Status’;
$form_questions_labels_required[]=’Tos’;

$text_fields_labels = array(‘First Name’,’Middle Name’,’Surname’);
$radio_buttons_labels = array(‘Gender’,’Tos’);
$drop_downs_labels = array(‘Marital Status’,’Working Status’);
/*
$i=1;
$options_radio_button_[$i] = array(‘Male’,’Female’,’Male To Female’,’Female To Male’);
$i=2;
$options_radio_button_[$i] = array(‘Yes’,’No’);
*/
/*
$i=1;
$options_drop_down_[$i] = array(‘Single’,’Married’,’Divorced’,’Widow’);
$i=2;
$options_drop_down_[$i] = array(‘Selfemployed’,’Employed’,’Unemployed’);
*/
//Gender Options
$i=1;
$options_radio_button_[$i][]=’Male’;
$options_radio_button_[$i][]=’Female’;
$options_radio_button_[$i][]=’Male To Female’;
$options_radio_button_[$i][]=’Female To Male’;
$total_options_radio_button_[$i] = count($options_radio_button_[$i]);//4
//Tos Options
$i=2;
$options_radio_button_[$i][]=’Yes’;
$options_radio_button_[$i][]=’No’;
$total_options_radio_button_[$i] = count($options_radio_button_[$i]);//2
//Marital Status Options
$i=1;
$options_drop_down_[$i][]=’Single’;
$options_drop_down_[$i][]=’Married’;
$options_drop_down_[$i][]=’Divorced’;
$options_drop_down_[$i][]=’Widow’;
$total_options_drop_down_[$i] = count($options_drop_down_[$i]);//4
//Working Status Options
$i=2;
$options_drop_down_[$i][]=’Selfemployed’;
$options_drop_down_[$i][]=’Employed’;
$options_drop_down_[$i][]=’Unemployed’;
$total_options_drop_down_[$i] = count($options_drop_down_[$i]);//3

$total_form_questions_labels = 9;
$total_form_questions_labels_required = 8;
$total_text_fields_labels = 3;
$total_radio_buttons_labels = count($radio_buttons_labels);//2
$total_drop_downs_labels = count($drop_downs_labels);//2
?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>?session_type=<?php echo $_SESSION[‘session_type’];?>” method=”post” enctype=”plain/text”>
<?php

foreach($form_questions_labels as $form_question_label) //Loop through the whole ‘Form Questions’ array.
{
$value = $form_question_label;
$value_1 = str_replace(” “,”_”,”$value”); //Replaced Spaces to Underscores.
$value_2 = strtolower(“$value_1”); //Replaced Upper Case to Lower Case.

//Check if current ‘Form Item’ is a ‘Text Field’ or not.
if(in_array(“$form_question_label”,$text_fields_labels)) //Current ‘Form Item’ proved to be a ‘Text Field’.
{
//Check if current ‘Form Item’ (Text Field) is a ‘required’ one or not.
if(in_array(“$form_question_label”,$form_questions_labels_required))//Current ‘Form Item’ (Text Field) proved to be a ‘required’ one.
{
//Added ‘*’ (asterisk) to indicate the ‘Text Field’ is a ‘required’ one.
echo “<label for=”$value_2”>$form_question_label *:</label>
<input type=”text” name=”$value_2″ placeholder=”$form_question_label”>”;
}
else
{
//Added no ‘*’ (asterisk) to indicate the ‘Text Field’ is NOT a ‘required’ one.
echo “<label for=”$value_2”>$form_question_label:</label>
<input type=”text” name=”$value_2″ placeholder=”$form_question_label”>”;
}
echo “<br>”;
}
//Check if current ‘Form Item’ is a ‘Radio Button’ or not.
if(in_array(“$form_question_label”,$radio_buttons_labels)) //Current ‘Form Item’ proved to be a ‘Radio Button’.
{
//Check if current ‘Form Item’ (Radio Button) is a ‘required’ one or not.
if(in_array(“$form_question_label”,$form_questions_labels_required))//Current ‘Form Item’ (Radio Button) proved to be a ‘required’ one.
{
//Added ‘*’ (asterisk) to indicate the ‘Radio Button’ is a ‘required’ one.
echo “<label for=”$value_2″>$form_question_label *:</label>”;
}
else
{
//Added no ‘*’ (asterisk) to indicate the ‘Radio Button’ is NOT a ‘required’ one.
echo “<label for=”$value_2″>$form_question_label:</label>”;
}
$i = 0;
foreach($radio_buttons_labels as $radio_button_label) //$radio_buttons_labels = (‘Gender’,’Tos’);
{
if($form_question_label == $radio_button_label) //eg. ‘Gender’.
{
$i++;
foreach($options_radio_button_[$i] as $option_radio_button_[$i])
{
echo “<input type=”radio” id=”$option_radio_button_[$i]” name=”$value_2″ value=”$option_radio_button_[$i]”>
<label_for=”$option_radio_button_[$i]”>$option_radio_button_[$i]</label>”;
}
echo “<br>”;
}
$i++;
}
}
//Check if current ‘Form Item’ is a ‘Drop Down’ or not.
if(in_array(“$form_question_label”,$drop_downs_labels)) //Current ‘Form Item’ proved to be a ‘Drop Down’.
{
//Check if current ‘Form Item’ (Drop Down) is a ‘required’ one or not.
if(in_array(“$form_question_label”,$form_questions_labels_required))//Current ‘Form Item’ (Drop Down) proved to be a ‘required’ one.
{
//Added ‘*’ (asterisk) to indicate the ‘Drop Down’ is a ‘required’ one.
echo “<label for=”$value_2″>$form_question_label *:</label>”;
}
else
{
//Added no ‘*’ (asterisk) to indicate the ‘Drop Down’ is NOT a ‘required’ one.
echo “<label for=”$value_2″>$form_question_label:</label>”;
}
$i = 0;
foreach($drop_downs_labels as $drop_down_label) //$drop_downs_labels = (‘Gender’,’Tos’);
{
if($form_question_label == $drop_down_label) //eg. ‘Gender’.
{
$i++;
echo “<select name=”$value_2″>”;
foreach($options_drop_down_[$i] as $option_drop_down_[$i])
{
echo “<option value=”$option_drop_down_[$i]”>$option_drop_down_[$i]</option>”;
}
echo “</select>”;
echo “<br>”;
}
$i++;
}
}
}
?>
<input type=”submit” name=”submit_personal_details” value=”Submit”>
<?php
//$current_function = __FUNCTION__;
//echo $current_function;

if($_SERVER[‘REQUEST_METHOD’] === ‘POST’)
{echo “Line 238<br>”;
if(isset($_POST[‘submit_personal_details’]) && $_SESSION[‘session_step’] != ‘end’)
{echo “Line 240<br>”;
//$_SESSION[‘session_step’] = ‘end’;
echo $_SESSION[‘session_step’];
foreach($form_questions_labels_required AS $form_question_label_required)
{
$value = $form_question_label_required;
$value_1 = str_replace(” “,”_”,”$value”); //Replaced Spaces to Underscores.
$value_2 = strtolower(“$value_1”); //Replaced Upper Case to Lower Case.

if(!isset($_POST[“$value_2”]) && empty($_POST[“$value_2”]))
{echo “Line 253!<br>”;//echo $_POST[‘form_question_label_required’];
//die(“Fill-in All required Form Fields that! Fields with asterisks * are required to be filled-in!”);
}
else
{
echo “$value_2<br>”; echo “Line 258!<br>”;
}
/*
if(!isset($_POST[‘first_name’]) && empty($_POST[‘first_name’]))
{echo “Line 247<br>”;echo $_POST[‘title’];
//die(“Fill-in All required Form Fields that! Fields with asterisks * are required to be filled-in!”);
}
else
{
echo “$value_2<br>”; echo “Line 252<br>”;
}
*/
}
$_SESSION[‘session_step’] = ‘end’;
echo $_SESSION[‘session_step’]; echo “<br>”;
echo “Line 273<br>”;
session_destroy();
echo session_status(); echo “<br>”; echo “Line 270<br>”;
}
}
}
}
?>
[/code]

What do you make out of all this ?
Don’t ask me about the sessions. Unless they got something to do with this issue. Like start and destroy session lines got something to do with it.

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-05-08/1588932977-382646-condition-error.png]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@tracknutMay 08.2020 — Echo out $form_question_label_required at the top of the foreach loop, and $value_2 at line 253, and see what they are. You're in a loop, so the fact that at some point you hit both line 253 and line 258 doesn't mean they were hit on the same iteration through the loop.
Copy linkTweet thisAlerts:
@NogDogMay 08.2020 — > @tracknut#1618198 You're in a loop, so the fact that at some point you hit both line 253 and line 258 doesn't mean they were hit on the same iteration through the loop.

👍️
Copy linkTweet thisAlerts:
@developer_webauthorMay 09.2020 — @tracknut#1618198

I did as you asked. Look:
<i>
</i>if($_SERVER['REQUEST_METHOD'] === 'POST')
{echo "Line 238&lt;br&gt;";
if(isset($_POST['submit_personal_details']) &amp;&amp; $_SESSION['session_step'] != 'end')
{echo "Line 240&lt;br&gt;";
//$_SESSION['session_step'] = 'end';
echo $_SESSION['session_step'];
foreach($form_questions_labels_required AS $form_question_label_required)
{
echo $form_question_label_required;
$value = $form_question_label_required;
$value_1 = str_replace(" ","_","$value"); //Replaced Spaces to Underscores.
$value_2 = strtolower("$value_1"); //Replaced Upper Case to Lower Case.

<i> </i> if(!isset($_POST["$value_2"]) &amp;&amp; empty($_POST["$value_2"]))
<i> </i> {echo "$value_2"; echo "Line 253!&lt;br&gt;";//echo $_POST['form_question_label_required'];
<i> </i> //die("Fill-in All required Form Fields that! Fields with asterisks * are required to be filled-in!");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "$value_2&lt;br&gt;"; echo "Line 258!&lt;br&gt;";
<i> </i> }
<i> </i> }
<i> </i> $_SESSION['session_step'] = 'end';
<i> </i> echo $_SESSION['session_step']; echo "&lt;br&gt;";
<i> </i> echo "Line 273&lt;br&gt;";
<i> </i> session_destroy();
<i> </i> echo session_status(); echo "&lt;br&gt;"; echo "Line 270&lt;br&gt;";
<i> </i> }
<i> </i>

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-05-09/1589018115-902122-puzzle.png]

The thing is, I did not fill-in any item in the form. And so, on the foreach loop, the php finds all $_POSTS not filled.

Remember that, on each iteration on the foreach loop, if php finds the $_
POST item is NOT filled then it should trigger the ELSE and echo "Line 258!". Meaning, current form item is filled.

Else, if it finds on the iteration on the foreach loop, the $_POST item is filled then it should trigger the IF and echo "Line 253!". Meaning, current form item is filled.

Now, since I did not fill-in any form item and left all of them blank then on the whole foreach loop, on all the loop iteration, the IF should have got triggered echoing line "253!". And not trigger the ELSE now and then.

If no text box was filled and no radio button checked and no drop down selected then I made no inputs and so the IF should have got triggered echoing "Line 253!" That was my point when i opened this thread.

And as I write this post, I was gonna make that same claim again hinting php is buggy but a thought just struck my mind and before I hint that claim I must test something out.

Ok, this is my thought ...

Even though I did not select any item in the radio buttons no in the drop downs, by default they are set to values. maybe, php is counting them values as my inputs ? Anyway, I'll update you folks once my experiment is finished.

Thanks for your helps!
Copy linkTweet thisAlerts:
@developer_webauthorMay 09.2020 — I just noticed something in the img ....

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-05-09/1589019162-175507-puzzle.png]

Look at it. I filled in nothing in the form.

But look at the text fields 'First Name", "Surname". They are not filled.

Php is echoing "Line 258!" for them, hinting them fields have been filled-in.

It just occured to me to ask you folks, if the text field's

value="" (nothing)

but the ....

placeholder="something"

then does the ....

value="something"

even if no input has been made ? If the value does equal to the placeholder value then now I can understand why the ELSE is getting triggered echoing "Line 258!" hinting text field has been filled.

I need your answer on this question.

Now, look at the radio buttons. No options selected and no options checked by default and so I can understand why php is counting them 2 form items as left blank. No troubles here.

Now let's look at the drop downs "Marital Status" and 'Working Status". Unlike the radio button situation, php is counting the drop downs to be selected even if i made no inputs. They are by default set to what you see in img. And so, even if I made no selections and expected php to count here as no inputs been made, I can now understand why php is seeing things differently. Seeing their default values as my inputs.

Correct ?

Anyway, I will wait for your inputs.

Folks, I need to fix the text fields. Fix them so the placeholder values do not get counted as an input from the user and does not count the value = "placeholder" value. And so, how to fix this ? Do answer this. Important. Show me any code snippets as example if you can.

And I need to fix the drop downs too so their is an empty option at the top of all options and that empty option gets counted as "value=""(nothing).

Just gonna change this:
<i>
</i>foreach($drop_downs_labels as $drop_down_label) //$drop_downs_labels = ('Gender','Tos');
{
if($form_question_label == $drop_down_label) //eg. 'Gender'.
{
$i++;
echo "&lt;select name="$value_2"&gt;";
foreach($options_drop_down_[$i] as $option_drop_down_[$i])
{
echo "&lt;option value="$option_drop_down_[$i]"&gt;$option_drop_down_[$i]&lt;/option&gt;";
}
echo "&lt;/select&gt;";
echo "&lt;br&gt;";
}
$i++;
}

to this ...

<i>
</i>echo "&lt;select name=""&gt;";
foreach($drop_downs_labels as $drop_down_label) //$drop_downs_labels = ('Marital Status','Working Status');
{
if($form_question_label == $drop_down_label) //eg. 'Gender'.
{
$i++;
echo "&lt;select name="$value_2"&gt;";
foreach($options_drop_down_[$i] as $option_drop_down_[$i])
{
echo "&lt;option value="$option_drop_down_[$i]"&gt;$option_drop_down_[$i]&lt;/option&gt;";
}
echo "&lt;/select&gt;";
echo "&lt;br&gt;";
}

<i> </i> $i++;
<i> </i> }


I added the following line just above the foreach loop:
<i>
</i>echo "&lt;select name=""&gt;";


Now gonna test. if you folks have a better way of fixing the drop down then let me know.

And don't forget to answer my other questions in this post.

Thanks!
Copy linkTweet thisAlerts:
@developer_webauthorMay 09.2020 — Nope! This completely erased all the drop down options ....
<i>
</i> $i = 0;
echo "&lt;select name="$value_2"&gt;"; //I HAVE PUT THIS HERE AS A TEST. TEST FAIL! IT ERASED ALL THE DROP DOWN OPTIONS! NEED TO DELETE THIS LINE!
foreach($drop_downs_labels as $drop_down_label) //$drop_downs_labels = ('Marital Status','Working Status');
{
if($form_question_label == $drop_down_label) //eg. 'Gender'.
{
$i++;
echo "&lt;select name="$value_2"&gt;";
foreach($options_drop_down_[$i] as $option_drop_down_[$i])
{
echo "&lt;option value="$option_drop_down_[$i]"&gt;$option_drop_down_[$i]&lt;/option&gt;";
}
echo "&lt;/select&gt;";
echo "&lt;br&gt;";
}
$i++;
}


TEST FAIL!

Another attempt ...
<i>
</i> $i = 0;
foreach($drop_downs_labels as $drop_down_label) //$drop_downs_labels = ('Marital Status','Working Status');
{
if($form_question_label == $drop_down_label) //eg. 'Gender'.
{
$i++;
echo "&lt;select name="$value_2"&gt;";
echo "&lt;option value="$option_drop_down_[$i]"&gt;$option_drop_down_[$i]&lt;/option&gt;"; //ADDED THIS LINE JUST ABOVE THE 2ND FOREACH LOOP AND THE FIRST DROP DOWN OPTIONS ARE BLANK AS INTENDED! YEEHA!
foreach($options_drop_down_[$i] as $option_drop_down_[$i])
{
echo "&lt;option value="$option_drop_down_[$i]"&gt;$option_drop_down_[$i]&lt;/option&gt;";
}
echo "&lt;/select&gt;";
echo "&lt;br&gt;";
}
$i++;
}
}


Ok. This final code fixed the drop down issues. I now see blank options at the top of each drop down option and if I select no option then php counts that as no inputs made. Triggers the IF and echoes "Line 253" as expected.

Now, need to fix the text field issue. Any ideas how to get php not count the placeholder value as the user's input ? Must see placeholder value and "value=" as separate values and not one. I don't want to rid the placeholders though.

I added the
<i>
</i>value=""&gt;

after the ...
<i>
</i>placeholder="$form_question_label"&gt;


But no luck.

Look at context to what I just did ...
<i>
</i>if(in_array("$form_question_label",$text_fields_labels)) //Current 'Form Item' proved to be a 'Text Field'.
{
//Check if current 'Form Item' (Text Field) is a 'required' one or not.
if(in_array("$form_question_label",$form_questions_labels_required))//Current 'Form Item' (Text Field) proved to be a 'required' one.
{
//Added '*' (asterisk) to indicate the 'Text Field' is a 'required' one.
echo "&lt;label for="$value_2"&gt;$form_question_label *:&lt;/label&gt;
&lt;input type="text" name="$value_2" placeholder="$form_question_label" value = ""&gt;"; CHECK THE END OF THIS LINE.
}
else
{
//Added no '*' (asterisk) to indicate the 'Text Field' is NOT a 'required' one.
echo "&lt;label for="$value_2"&gt;$form_question_label:&lt;/label&gt;
&lt;input type="text" name="$value_2" placeholder="$form_question_label"&gt;";
}
echo "&lt;br&gt;";
Copy linkTweet thisAlerts:
@developer_webauthorMay 10.2020 — No one knows the answers ? 😁
Copy linkTweet thisAlerts:
@NogDogMay 10.2020 — > @developer_web#1618196

> if(!isset($_POST["$value_2"]) &amp;&amp; empty($_POST["$value_2"]))


What I believe you actually wanted is an "or" condition...
<i>
</i>if(!isset($_POST[$value2]) || empty($_POST[$value_2]))

...and that is redundant, since [u][empty()](https://php.net/empty)[/u] includes the isset() bit; so you could just do:
<i>
</i>if(empty($_POST[$value_2]))

However, note that "0" is considered "empty" while " " is not (due to PHP's loose typing); so you might want to do:
<i>
</i>if(!isset($_POST[$value_2]) || trim($_POST[$value_2]) === '') // === is important here
Copy linkTweet thisAlerts:
@developer_webauthorMay 11.2020 — @NogDog#1618298

Thanks!

So, you are saying, if there is a risk that ($_POST[$value_2] =='0'; and I do not want to count the '0' value as an 'empty value' (form item not filled-in) then best to use the "if(!isset($_POST[$value_2])" here instead ?

I keep forgetting these basic things about the "0" counted on some places as this while on other places as that! Thanks for reminding but do answer my question to confirm.
Copy linkTweet thisAlerts:
@NogDogMay 11.2020 — Yes, a zero will be considered "empty", while a field with one or more space (or other white-space) characters will not be considered "empty".
Copy linkTweet thisAlerts:
@developer_webauthorMay 12.2020 — @NogDog#1618337

Php is sort of ridiculous isn't it ? I would not consider any value as empty be they 0 or white space.

You may close this thread as SOLVED.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...