/    Sign up×
Community /Pin to ProfileBookmark

Why Not Getting Undefined Index Error ?

Folks,

Look at this code. It gives no error:

““
//WORKING FINE
session_start();
echo session_id();
echo “<br>”;
echo __LINE__;
echo “<br>”;

if(!session_id() || !isset($_SESSION[‘form_step’]) || $_SESSION[‘form_step’] != ‘end’)
{
echo __LINE__;
echo “<br>”;
$_SESSION[‘form_step’] = ‘start’;
echo $_SESSION[‘form_step’];
echo “<br>”;
echo session_id();
echo “<br>”;
$_SESSION[‘form_step’] = ‘end’;
echo $_SESSION[‘form_step’];
echo “<br>”;
echo __LINE__;
echo “<br>”;
}
elseif($_SESSION[‘form_step’] == ‘end’)
{
echo __LINE__;
echo “<br>”;
echo $_SESSION[‘form_step’];
echo “<br>”;
echo session_id();
echo “<br>”;
echo __LINE__;
echo “<br>”;
}
““

And look at this one that gives error:
**Notice: Undefined index: form_step in C:xampphtdocspower.pagepagination_test_SIMPLE.php on line 143**

““
//WHY GIVES UDNEFINED ERROR ON LINE: elseif($_SESSION[‘form_step’] == ‘end’)
session_start();
echo session_id();
echo “<br>”;
echo __LINE__;
echo “<br>”;

if(!session_id())
{
if(!isset($_SESSION[‘form_step’]) || $_SESSION[‘form_step’] != ‘end’)
{
echo __LINE__;
echo “<br>”;
$_SESSION[‘form_step’] = ‘start’;
echo $_SESSION[‘form_step’];
echo “<br>”;
echo session_id();
echo “<br>”;
$_SESSION[‘form_step’] = ‘end’;
echo $_SESSION[‘form_step’];
echo “<br>”;
echo __LINE__;
echo “<br>”;
}
}
elseif($_SESSION[‘form_step’] == ‘end’)
{
echo __LINE__;
echo “<br>”;
echo $_SESSION[‘form_step’];
echo “<br>”;
echo session_id();
echo “<br>”;
echo __LINE__;
echo “<br>”;
}
die;
““

Both codes are practically the same. I just changed this line from the 1st code:

““
if(!session_id() || !isset($_SESSION[‘form_step’]) || $_SESSION[‘form_step’] != ‘end’)
{
““

to this line in the 2nd code:

““
if(!session_id())
{
if(!isset($_SESSION[‘form_step’]) || $_SESSION[‘form_step’] != ‘end’)
{
““

That is all. The 2nd code giving the error.
Q1. How come the 1st code is not giving that same error ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 22.2020 — Are you sure it's not the elseif that's failing? (That seems more likely based on the fact that if you get there, $_SESSION ['form_step'] is not necessarily set, as it's else-ing the if(!session_id()) block.
Copy linkTweet thisAlerts:
@developer_webauthorJun 24.2020 — @NogDog#1619792

I understand now that on the 1st code, the:

if(!session_id()

is not triggering the IF but the:

!isset($_SESSION['form_step']) || $_SESSION['form_step'] != 'end')'

is triggering it and during the trigger, the:

$_SESSION['form_step']

is getting defined.

Hence, when I refresh the page, on the 2nd round the ELSEIF is getting triggered and it finds the:

$_SESSION['form_step']

defined.

Hence, shows no problem.

While with the 2nd code, unlike the 1st code, the ELSEIF is getting triggered first and it finds the:

$_SESSION['form_step']

not defined yet and so gives the error.

You see, I thought on the 1st code, the:

if(!session_id()

was triggering the IF. But it actually wasn't. That wrong thinking of mine was taking me off track to confusion!

Thanks!
×

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 4.25,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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