LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2016, 04:40 AM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
PHP script beginner


Hi PHP guru's

I've started playing on a php scripting following cbt video of Dean Davis. I'm using now XAMPP as my testing area of the codes. For some reason there is a section on the video where I'm testing the UNSET variable and brings me this error:

Code:
Notice: Undefined index: version in C:\xampp\htdocs\qq.php on line 18
strangely from the video provided it should not throw an error.

This is the code I'm using:

PHP Code:
<?

$arr1 
= array("browser" => "mozilla""agent" => "firefox");
echo 
$arr1["browser"], "\t"$arr1["agent"];

$arr1["version"] = "0.9.1";
echo 
$arr1["version"];

$arr1["browser"] = "netscape";
echo 
"<p>"$arr1["browser"];

unset(
$arr1["version"]);
echo 
$arr1["version"];

?>

</body>
</html>
 
Old 05-02-2016, 05:06 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,111
Blog Entries: 3

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Which distro are you using? It would be easier for you and easier for people to help you if you work with the normal LAMP stack which is provided in your distro's repository. The directions vary depending on which distro you have but they are all quick and low-effort to install.

XAMPP is for Windows and those that try to shoehorn it onto GNU/Linux find that it costs a lot of extra work and headache.

That said, if I recall PHP, the blocks of code should start with '<?php' instead of '<?' alone.
 
Old 05-02-2016, 05:21 AM   #3
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Which distro are you using? It would be easier for you and easier for people to help you if you work with the normal LAMP stack which is provided in your distro's repository. The directions vary depending on which distro you have but they are all quick and low-effort to install.

XAMPP is for Windows and those that try to shoehorn it onto GNU/Linux find that it costs a lot of extra work and headache.

That said, if I recall PHP, the blocks of code should start with '<?php' instead of '<?' alone.
I'm using windows 7 with xampp lite v3.2.2

with regards on the <?php instead of '<?' I already managed to enable short tags on and worked out
 
Old 05-02-2016, 05:43 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,016
Blog Entries: 5

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
You've unset the variable (destroyed it) and then asked PHP to print the variable out. It can't do this because you've just unset it, it doesn't exist any more.

P.S. http://php.net/manual/en/function.array-key-exists.php

Last edited by hydrurga; 05-02-2016 at 05:46 AM.
 
Old 05-02-2016, 05:50 AM   #5
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
You've unset the variable (destroyed it) and then asked PHP to print the variable out. It can't do this because you've just unset it, it doesn't exist any more.

P.S. http://php.net/manual/en/function.array-key-exists.php
Is there a way that I can eliminate this:

Code:
Notice: Undefined index: version in C:\xampp\htdocs\qq.php on line 18

I'm following the tutorial video and would like to sync the outputs the same
 
Old 05-02-2016, 06:06 AM   #6
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19.1 MATE
Posts: 8,016
Blog Entries: 5

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
I have no idea what the video is telling you to do, apss_evaluator. All I know is that you've unset an array element and then you're trying to print that array index, hence the error. PHP can't find the index "version" in the $arr1 array because it doesn't exist any more.

You can test for the existence of an array index using the function I referred to.
 
Old 05-02-2016, 06:33 AM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
You could disable notice report
PHP Code:
error_reporting(E_ALL & ~E_NOTICE);
unset(
$arr1["version"]);
echo 
$arr1["version"]; 
But as hydrurga said, the version key doesn't exist anymore so what the point trying to echo this non existent item
 
Old 05-02-2016, 08:46 AM   #8
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
You could disable notice report
PHP Code:
error_reporting(E_ALL & ~E_NOTICE);
unset(
$arr1["version"]);
echo 
$arr1["version"]; 
But as hydrurga said, the version key doesn't exist anymore so what the point trying to echo this non existent item
The video tells when it echo the unsetted key pair it will show nothing beside the "netscape"

simply the results should be like this:

Code:
mozilla	firefox0.9.1
netscape

instead of this I'm getting:

Code:
mozilla	firefox0.9.1
netscape
Notice: Undefined index: version in C:\xampp\htdocs\qq.php on line 18
 
Old 05-02-2016, 09:29 AM   #9
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 11.0
Posts: 165

Rep: Reputation: 181Reputation: 181
Quote:
Originally Posted by apss_evaluator View Post
instead of this I'm getting:

Code:
mozilla	firefox0.9.1
netscape
Notice: Undefined index: version in C:\xampp\htdocs\qq.php on line 18
This is the expected output. I tested in PHP 5.5.6 and got the same result. I see what he's trying to prove - that if you unset the variable, then there's nothing to display. That's essentially what you successfully proved by receiving that notice.

Either the instructor has notice reporting turned off somewhere (as keefaz suggested), or perhaps he's using an older version of PHP that doesn't throw the notice. I can't tell you when or if there was a version where this notice wouldn't show up, but I don't remember there ever being a case where what you're trying to do didn't throw that notice.
 
1 members found this post helpful.
Old 05-02-2016, 10:30 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Quote:
Originally Posted by apss_evaluator View Post
simply the results should be like this:
Why it should be like this? echo'ing a non existent array item leads to unknown, your php setup throws notice on standard out, while in some other configs it will seem to be silent (like in your video) but notice will be seen on server errors log etc...

Imho it isn't good programming practice to print null variable, in some other language, this error could lead to quit program abruptly.

Last edited by keefaz; 05-02-2016 at 10:32 AM.
 
Old 05-02-2016, 10:33 AM   #11
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by urbanwks View Post
This is the expected output. I tested in PHP 5.5.6 and got the same result. I see what he's trying to prove - that if you unset the variable, then there's nothing to display. That's essentially what you successfully proved by receiving that notice.

Either the instructor has notice reporting turned off somewhere (as keefaz suggested), or perhaps he's using an older version of PHP that doesn't throw the notice. I can't tell you when or if there was a version where this notice wouldn't show up, but I don't remember there ever being a case where what you're trying to do didn't throw that notice.

You're right the instructor is using php 5.0.2, appears to be way back old version. understood it now. thanks :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a beginner's PHP/MySQL script: MySQL in spreadsheet-like layout julianb LinuxQuestions.org Member Success Stories 2 01-01-2010 08:25 PM
A beginner's doubt in PHP vivekr Programming 7 02-06-2007 03:26 PM
LXer: Title: PHP/MySQL Classifieds Script AddAsset1.php Script Insertion LXer Syndicated Linux News 0 07-02-2006 06:21 PM
Apache and PHP for total beginner legendaryhwk Linux - Newbie 6 02-23-2006 07:13 AM
PHP-Mysql Tutorial for beginner/dummies ewinandar Programming 1 11-10-2004 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration