Trouble working with php variables

software development

#1

I am trying to learn how to use PHP. The basic stuff works like a simple hello proram with the list of the installed PHP’s info http://www.tsgtoolbox.com/phptest/ph01/hello.php

The problem is comming into when I start using variables that are not defined within the PHP code but come through through the address.

http://www.tsgtoolbox.com/phptest/ph02/hiJacob.php has the following code and seems to work

Hi Jacob

Hi Jacob

Demonstrates using a variable

<? $userName = "Jacob"; print "Hi, $userName" ?>

but when I use the variable in the address it does not carry over to the code.
the page I am trying to do this on is
http://www.tsgtoolbox.com/phptest/ph02/hiUser.php?userName=PersonsName

and the code is

Hi User

Hi User

PHP program that receives a value from "whatsName"

<? print "

Hi there, $userName!

"; ?>

the output I am recieving is

Hi User
PHP program that receives a value from "whatsName"
Hi there, !

I literally copied the files over from a book that I had just recieved and everything had worked up to this point. Is there something I need to do with my settings in dreamhost, my brower, code, or is it a limitation of dreamhost?
Thank you to everyone who can offer assistance.


#2

You need a space between $username and the “!” - “$username” is a different variable than “$username!” .

Hi User

Hi User

PHP program that receives a value from "whatsName"

<? print "

Hi there, [color=#CC0000]$userName ![/color]

"; ?>

I guess you either copied it wrong, or the book’s editors were sloppy. :wink:

–rlparker


#3

Actually, that part should be okay in double quotes. I just did a test where I printed something like “sdf098dsf$variable!lkjsdf” and it worked.

It looks like he need to change:

print “

Hi there, $userName!

”;

to:

print “

Hi there, {$_GET[‘userName’]}!

”;

Since he’s pulling it from a query string.

To the OP: It’s a good habit to always call variables that are being passed with $_POST or $_GET, rather than relying on register_globals being on, which can vary between hosts. More on that here.


:stuck_out_tongue: Save up to $96 at Dreamhost with ALMOST97 promo code (I get $1).
Or save $97 with THEFULL97.


#4

You are right, it should, but when I tested it on my php 4.4.7 install, it borked (and adding the space “fixed” it :open_mouth: )!

That is, of course, much better for the reasons you explained. :wink:

–rlparker


#5

Thank you for the suggestion, seiler. Looks like that did the trick, and it looks like I will be jumping to chapter 5 in my book :wink:


#6

Cool. :wink:

Just curious… what book are you using and how old is it? What version of PHP does it seem to focus on?

If it’s an old one, you might run into a few more things that don’t work they way you’d expect.

Some of the books out there are somewhat outdated, or at least some editions are. You could search for your book title at amazon.com and see if the current edition is the same as yours.

PHP, MySQL & hosting in general constantly change/evolve, so it’s good to learn as much as possible, but also from an up-to-date source.

PHP.net’s manual is one of the best places to look for info. When you look up something there, you get the official explanation, a few examples, plus a bunch of user comments.


:stuck_out_tongue: Save up to $96 at Dreamhost with ALMOST97 promo code (I get $1).
Or save $97 with THEFULL97.


#7

I actually got the book on amazon
http://www.amazon.com/PHP-MySQL-P…

Once I saw that you mentioned about having Registered_Globals off I was like ‘ooh, that is why it isn’t working’ because the book has you turn them on for the first few chapters, and it says in chapter 5 it has more information on how to do stuff when it is off.
I am not too worried about this issue and will just adapt to it.


#8

I have that book, and I can honestly say that it is nine kinds of shite. I used to be an instructor at a local college and the Powers That Be tried to make me teach with that book (I refused). I definitely agree that PHP.net is by far the best resource for learning the language, but I also recommend Teach Yourself PHP in 24 hours (even though it is a little out of date) for the complete novice.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#9

That will work, but two things to remember: 1) check a variable before using it 2) don’t trust external data!

Personally, I would check if (isset($_GET[‘userName’])) and assign it to $username or similar. If it wasn’t set, it could be changed to “guest.” When displaying it, use htmlentities (or htmlspecialchars, etc.).

It’s a good idea to learn about basic security very early on so that you develop good habits. You’d be surprised how terrible most php scripts (and even tutorials) are when it comes to security.


Read my blog. You know you want to…


#10

Thats a good point, another option would be to notify the viewer that they failed to enter a username and press the back button to enter the remaining fields.
Silk

My website