Jump to content

Re: Data Types


Gummie

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]
Link to comment
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
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
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?!
Link to comment
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]
Link to comment
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
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.