Jump to content


Photo

Re: Data Types


  • Please log in to reply
19 replies to this topic

#1 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 12:45 PM

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:

<?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 />";
?>

According to the book, I should receive this in my browser:

NULL
integer
string
double
boolean


However, I actually receive the following:

Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_data_types.php on line 11

Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_data_types.php on line 12

NULL
integer
string
double
boolean


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

Edited by a moderator to put the [code][/code] tags in instead of [color=blue]

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 12:49 PM

try doing

var $testing;

instead of just $testing;

edited to add $ on my vars. also if that don't work then try

global $testing;
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 12:53 PM

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 code
NULL integer string double boolean

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 12:58 PM

(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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 12:59 PM

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.5

Ken

#6 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 01:01 PM

Thanks Crayon Violent and redarrow for the prompt replies.

I made the change: var $testing and I received a parse error.

However, when I made the change: global $testing, 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

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 01:02 PM

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 work
global $blah; //should work

i can't think of a reason why you really need to 'declare' your variables Gummie.  simply doing

$blah = 'hello';

declares it.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 01:05 PM

Correct you got to setup the php .ini correctly on install but a can not dam remember the condititon to change.

sorry.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 01:05 PM

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?

Thanks

Gummie

#10 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 01:12 PM

By the way, I have a similar problem if I use the var_dump function instead of gettype.

e.g.
<?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 />";
?>

with the corresponding output:

Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_var_dump.php on line 11

Notice: Undefined variable: testing in f:\easyphp1-8\www\+ learning\courses\sams 24 hrs\hour 04\04_var_dump.php on line 12

NULL
int(5)
string(4) "five"
float(5)
bool(true)


So I guess it has something to do with the way PHP is setup to deal with unassigned variables?!

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 09 July 2006 - 01:21 PM

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.
<?

$name="redarrow";
echo $name;

echo "<br>";

$redarrow=$name;
echo $name;

echo"<br>";

$name=$name;
echo $name;

echo "<br>";

$name='redarrow';
echo $name;

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 01:21 PM

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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 01:27 PM

When I turn on all error reporting and use this 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 />";
?>
I get a notice of an undefined variable: testing in line 4

When you declare the variable to be gobal, that is defining the variable so it's no longer undefined.

Ken

#14 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 01:29 PM

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

#15 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 09 July 2006 - 01:31 PM

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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#16 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 July 2006 - 01:32 PM

no Gummie see that's the thing. you should see from your own example that you don't 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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#17 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 01:45 PM

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

#18 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 03:12 PM

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.


If you are using EasyPHP as specified at the beginning of this post, do the following:

Go to the directory: "C:\EasyPHP1-8\conf_files\php.ini" 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:

error_reporting  =  ...

and

display_errors = ...

Amend these to:

error_reporting  =  E_ALL & ~E_NOTICE

and

display_errors = On

Then save your file, reboot your system and the changes should take affect.



Best regards to all,

Gummie

#19 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 03:41 PM

You don't have to reboot your system, just shutdown and restart the Apache webserver process.

Ken

#20 Gummie

Gummie
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 09 July 2006 - 04:38 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users