Gummie Posted July 9, 2006 Share Posted July 9, 2006 Hi Everybody,I hope someone can help me because I'm a newbie. I've been learning PHP for a short while now, but it's been rather haphazard so I decided to learn it properly. As such, I'm studying from Sam's Teach Yourself PHP in 24 hours (PHP ver 4.3) using EasyPHP 1.8. Everything appears to have been installed hunky-dory without any errors whatsoever.My problem is this; I've written some code directly from the book (a chapter on DATA TYPES). This is it:[code]<?php $testing; // Declare without assigning. print gettype( $testing ); // NULL print "<br />"; $testing = 5; print gettype( $testing ); // Integer print "<br />"; $testing = "five"; print gettype( $testing ); // String print "<br />"; $testing = 5.0; print gettype( $testing ); // Double print "<br />"; $testing = true; print gettype( $testing ); // boolean print "<br />";?>[/code]According to the book, I should receive this in my browser:[color=blue]NULLintegerstringdoubleboolean[/color]However, I actually receive the following:[color=red]Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_data_types.php on line 11Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_data_types.php on line 12[/color][color=blue]NULLintegerstringdoubleboolean[/color]Can anyone please explain why this is happening and what I can do to prevent the error message? Reference texts state that I can declare a variable without assigning an initial value to it, so why then am I receiving an error message?It's probably a very simple issue for the experienced, but it's bugging me.Thanks very much in advance for any help anyone can offer,Gummie[b]Edited by a moderator to put the [nobbc][code][/code][/nobbc] tags in instead of [nobbc][color=blue][/nobbc][/b] Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/ Share on other sites More sharing options...
.josh Posted July 9, 2006 Share Posted July 9, 2006 try doingvar $testing;instead of just $testing;edited to add $ on my vars. also if that don't work then try global $testing; Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55101 Share on other sites More sharing options...
redarrow Posted July 9, 2006 Share Posted July 9, 2006 The code is fine i tested it and the book is correct ok.<?php $testing; // Declare without assigning. print gettype( $testing ); // NULL print ""; $testing = 5; print gettype( $testing ); // Integer print ""; $testing = "five"; print gettype( $testing ); // String print ""; $testing = 5.0; print gettype( $testing ); // Double print ""; $testing = true; print gettype( $testing ); // boolean print "";?>result from codeNULL integer string double boolean Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55104 Share on other sites More sharing options...
ShogunWarrior Posted July 9, 2006 Share Posted July 9, 2006 (PHP 4.3) I too get an error about an undefined variable.I think that error is conclusively replicated then, but I'm not sure was it the difference in PHP version that's the problem, maybe in 4.0 or previous that was valid, or maybe in 5.0 up, not sure. Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55108 Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 What is line 11 and 12?BTW, I copied you code to my machine (Windows XP running xampp) and it works fine. PHP 5.0.5Ken Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55109 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 Thanks Crayon Violent and redarrow for the prompt replies.I made the change: [color=red]var $testing[/color] and I received a parse error.However, when I made the change: [color=blue]global $testing[/color], everything works fine.In addition, this only works if global is declared only the first time the variable is declared.Thanks Crayon but can anyone explain why this is happening? What if I just want to declare a local variable, what do I do then? I can't always use a global variable.Gummie Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55111 Share on other sites More sharing options...
.josh Posted July 9, 2006 Share Posted July 9, 2006 i think the problem is a setting in php.ini that is on or off by default when you first install php to your machine. i'm not 100% on that tho.var $blah; //may or may not workglobal $blah; //should worki can't think of a reason why you really need to 'declare' your variables Gummie. simply doing $blah = 'hello'; declares it. Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55112 Share on other sites More sharing options...
redarrow Posted July 9, 2006 Share Posted July 9, 2006 Correct you got to setup the php .ini correctly on install but a can not dam remember the condititon to change.sorry. Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55115 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 Don't worry about the line numbers, I have other standard HTML in the document so the code may not appear to match the line numbers as I have only given the PHP coding.And does anyone know how I should configure the ini file to solve this issue?ThanksGummie Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55116 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 By the way, I have a similar problem if I use the [color=blue]var_dump[/color] function instead of [color=blue]gettype[/color].e.g.[code]<?php $testing; // Declare without assigning. print var_dump( $testing ); // NULL print "<br />"; $testing = 5; print var_dump( $testing ); // int(5) print "<br />"; $testing = "five"; print var_dump( $testing ); // string(4) "five" print "<br />"; $testing = 5.0; print var_dump( $testing ); // float(5) print "<br />"; $testing = true; print var_dump( $testing ); // bool(true) print "<br />";?>[/code]with the corresponding output:[color=red]Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_var_dump.php on line 11Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_var_dump.php on line 12[/color][color=blue]NULL int(5) string(4) "five" float(5) bool(true)[/color]So I guess it has something to do with the way PHP is setup to deal with unassigned variables?! Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55120 Share on other sites More sharing options...
redarrow Posted July 9, 2006 Share Posted July 9, 2006 does this work if it does get programmming ok not all books are upto date that's why use the manual while studying in conjustion with any book ok.[code]<?$name="redarrow";echo $name;echo "<br>";$redarrow=$name;echo $name;echo"<br>";$name=$name;echo $name;echo "<br>";$name='redarrow';echo $name;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55125 Share on other sites More sharing options...
ShogunWarrior Posted July 9, 2006 Share Posted July 9, 2006 I went through the PHP INI and didn't find anything.I imagine that what some people are referring to is having ERROR_REPORTING level low so that these kind of errors aren't reported.What you should do is set the variable = NULL when you first declare it. Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55126 Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 When I turn on all error reporting and use this code:[code]<?php error_reporting(E_ALL); $testing; // Declare without assigning. print gettype( $testing ); // NULL print "<br />"; $testing = 5; print gettype( $testing ); // Integer print "<br />"; $testing = "five"; print gettype( $testing ); // String print "<br />"; $testing = 5.0; print gettype( $testing ); // Double print "<br />"; $testing = true; print gettype( $testing ); // boolean print "<br />";?>[/code]I get a notice of an undefined variable: testing in line 4When you declare the variable to be gobal, that is defining the variable so it's no longer undefined.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55130 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 Thanks everyone for your feedback,I took Shogun's advice and when I initially declare the variable as NULL, everything works fine. Also, no matter how I declare the variable, as long as it is initialised, I receive no errors. I guess I have to initialise all my variables before using them.However, where do I find ERROR_REPORTING and how can I amend it?Thanks,Gummie Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55131 Share on other sites More sharing options...
ShogunWarrior Posted July 9, 2006 Share Posted July 9, 2006 The error_reporting setting is in PHP.INI around line 288, open EasyPHP, click the Logo and go to PHP config and when you have edited the file save it and restart the server. Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55134 Share on other sites More sharing options...
.josh Posted July 9, 2006 Share Posted July 9, 2006 no Gummie see that's the thing. you should see from your own example that you [i]don't[/i] have to initialize your variables before using them. assigning a value to a variable automatically declares it. even with $blah = NULL; you are not really 'declaring' or 'initializing' it. you are assigning 'nothing' to it. Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55135 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 Point taken Crayon, thanks.And thanks also for the reference in the ini file. I'll play around with it and try to get the error reporting down a little.Gummie Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55141 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 Okay, first of all, thanks everybody for pointing me in the right direction.If anyone has the same problem, note the following for future reference.[color=red]If you are using EasyPHP as specified at the beginning of this post, do the following:Go to the directory: "[color=black]C:\EasyPHP1-8\conf_files\php.ini[/color]" or appropriate to wherever you have installed EasyPHP and open THIS file.I don't know how EasyPHP works, but it creates duplicate copies of this file in other folders for backup or whatever else. If you amend the text in the duplicates, the changes WILL NOT take effect when using EasyPHP. So, make sure you access ONLY this file and close all others.Then find the following section that contains:[color=black]error_reporting = ...[/color]and[color=black]display_errors = ...[/color]Amend these to:[color=black]error_reporting = E_ALL & ~E_NOTICE[/color]and[color=black]display_errors = On[/color]Then save your file, reboot your system and the changes should take affect.[/color]Best regards to all,Gummie Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55177 Share on other sites More sharing options...
kenrbnsn Posted July 9, 2006 Share Posted July 9, 2006 You don't have to reboot your system, just shutdown and restart the Apache webserver process.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55194 Share on other sites More sharing options...
Gummie Posted July 9, 2006 Author Share Posted July 9, 2006 Somebody did mention that earlier, but I found that restarting the server didn't do it for my system; perhaps it should.In any case, it was a sure thing once I rebooted.Gummie Quote Link to comment https://forums.phpfreaks.com/topic/14091-re-data-types/#findComment-55218 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.