Jump to content

Archived

This topic is now archived and is closed to further replies.

Gummie

Re: Data Types

Recommended Posts

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]NULL
integer
string
double
boolean[/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 11

Notice: 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]NULL
integer
string
double
boolean[/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]

Share this post


Link to post
Share on other sites
try doing

var $testing;

instead of just $testing;

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

global $testing;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
(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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


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

sorry.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 11

Notice: 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?!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 4

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

Ken

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You don't have to reboot your system, just shutdown and restart the Apache webserver process.

Ken

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.