/    Sign up×
Community /Pin to ProfileBookmark

Folks,

Why this shows undefined Variable since it is Super Global Variable ? It should always be filled with something, even blank. And not show error it in undefined.

[code]
<?php

foreach($php_errormssg as $error)
{
if(is_scalar($php_errormssg))
{
echo ‘Scalar Variable:’ .$global; echo ‘<br>’;
}
else
{
echo ‘Non-Scalar Variable:’ .print_r($global); echo ‘<br>’; //Shows error as Variable not Scalar Variable. So, don’t echo but print_r instead.
}
}

?>
[/code]

**Notice: Undefined variable: php_errormssg in C:xampphtdocsTemplatestest.php on line 3**

If the Global Variable $php_errormsg is empty then it should just show blank.
How to fix this ?

2nd Code
Look this following one works fine:

[code]
<?php

foreach($GLOBALS as $global)
{
if(is_scalar($GLOBALS))
{
echo ‘Scalar Variable:’ .$global; echo ‘<br>’;
}
else
{
echo ‘Non-Scalar Variable:’ .print_r($global); echo ‘<br>’; //Shows error as Variable not Scalar Variable. So, don’t echo but print_r instead.
}
}

?>
[/code]

I get echoed …
**Array ( ) Non-Scalar Variable:1
Array ( ) Non-Scalar Variable:1
Array ( [PHPSESSID] => tsdktj0t5umjptqdbgi73fnje0 ) Non-Scalar Variable:1
Array ( ) Non-Scalar Variable:1
Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( [PHPSESSID] => tsdktj0t5umjptqdbgi73fnje0 ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* [global] => Array *RECURSION* ) Non-Scalar Variable:1
Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( [PHPSESSID] => tsdktj0t5umjptqdbgi73fnje0 ) [_FILES] => Array ( ) [GLOBALS] => Array *RECURSION* [global] => Array *RECURSION* ) Non-Scalar Variable:1**

Is anything wrong with my 2nd code too ? Yes or no ?

And this 3rd code work fine too:

[code]
<?php

foreach($_SERVER as $serve)
{
echo $serve; echo ‘<br>’; //Shows error as Variable not Scalar Variable. So, don’t echo but print_r instead.
}

if(is_scalar($_SERVER))
{
echo ‘Is Scalar Variable.’;
}
else
{
echo ‘Is NOT Scalar Variable!’;
}

?>
[/code]

Tell me. Is anything wrong with this 3rd code too ?
If not then why 1st one fails ?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSAApr 16.2021 — @developer_web#1630459 What is the error message? Can you please share the snapshot with us?
Copy linkTweet thisAlerts:
@developer_webauthorApr 16.2021 — @VITSUSA#1630463

My code had a typo.

Here is to fixo (I invented a new word just now :fixo"):
<i>
</i>&lt;?php

foreach($php_errormssg as $error)
{
if(is_scalar($php_errormssg))
{
echo 'Scalar Variable:' .$error; echo '&lt;br&gt;';
}
else
{
echo 'Non-Scalar Variable:' .print_r($error); echo '&lt;br&gt;'; //Shows error as Variable not Scalar Variable. So, don't echo but print_r instead.
}
}

?&gt;

**Notice: Undefined variable: php_errormssg in C:xampphtdocsTemplatestest.php on line 3

Warning: Invalid argument supplied for foreach() in C:xampphtdocsTemplatestest.php on line 3**

If the Global Variable $php_errormsg is empty then it should just show blank.

How to fix this ?

Unable to attach anything on this forum.
Copy linkTweet thisAlerts:
@NogDogApr 16.2021 — Again, from the manual: https://www.php.net/manual/en/reserved.variables.phperrormsg.php

> Warning
>
> This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.
>
> Use error_get_last() instead.
>
> Description ¶
>
> $php_errormsg is a variable containing the text of the last error message generated by PHP. This variable will only be available within the scope in which the error occurred, **and only if the track_errors configuration option is turned on (it defaults to off)** [my emphasis].


So, basic thing to take from that: don't use it.
Copy linkTweet thisAlerts:
@tracknutApr 16.2021 — > @developer_web#1630465 foreach($php_errormssg as $error)

You have a spelling issue in "errormssg" also
Copy linkTweet thisAlerts:
@developer_webauthorApr 17.2021 — @tracknut#1630481

Fixing it didn't make any difference. Still thanks.

Anything else you want to add ?
Copy linkTweet thisAlerts:
@developer_webauthorApr 17.2021 — @NogDog#1630480

You forgot to mention it is deprecated. Good thing I followed your link to the God Forsaken manual. ;)

Now, wasn't $php_errormsg was supposed to be a Super Global ?

$error_get_last is a Super Global now, is it ? Let me check. If not, then let me know why they replaced a Super Global with a non-Super Global. Ok ?
Copy linkTweet thisAlerts:
@developer_webauthorApr 17.2021 — @tracknut,

Is my NOTE ok ?
<i>
</i>&lt;?php
//NOTE: Seems like $error_get_last() is not a Super Global. It is a replacement of the Super Global $php_errormsg.
foreach($error_get_last as $error)
{
if(is_scalar($error_get_last))
{
echo 'Scalar Variable:' .$error; echo '&lt;br&gt;';
}
else
{
echo 'Non-Scalar Variable:' ; echo $error; echo '&lt;br&gt;'; //Doing print_r here incase Variable is not Scalar. (Trying to echo Non-Scalar Variable shows error).
}
}

?&gt;
Copy linkTweet thisAlerts:
@tracknutApr 17.2021 — @developer_web#1630500

error_get_last() is a function. You can't just put a $ in front of it and treat it as a variable, you need to call it. Try this:
<i>
</i>&lt;?php
echo $a; // generate an error
$errors = error_get_last();

foreach($errors as $part)
{
if(is_scalar($errors))
{
echo "Scalar Variable: $part&lt;br&gt;";
}
else
{
echo "Non-Scalar Variable: $part&lt;br&gt;";
}
}
?&gt;
Copy linkTweet thisAlerts:
@developer_webauthorApr 18.2021 — @tracknut#1630507

Thanks.

My $ was a typo.
×

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,
)...