Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what does it mean" show.php?catid=1"?

Status
Not open for further replies.

eric2003

Technical User
Jun 25, 2003
1
0
0
CA
Hi,
I am learning PHP , but I do not know how to pass a parameter to another function. Here I have " show.php?catid=1" in one example, and it will call function show.php and pass one parameter "catid" to it. But when I run it, the parameter is not passed. The session has been started "seesion_start();", so wha is the problem?

Thanks

william
 
catid is not a session id...it is a vairable passed as you say in the URL...so you need to use

$catid=$_GET['catid'];

to retreive the var from the url bar and use it in a script

Bastien

cat, the other other white meat
 
Yeah, Bastien is right. And there is another thing.
The
Code:
show.php?catid=1
is not passing an argument to the function, since the show.php is not function but it is a script. Function is something like strtoupper() and similar. Functions have different mechanism to passing arguments into them because there are no ? and & characters...
 
if you hace 'global vars' swiched on, a variable by the in
the url strin will be automatically created !

ie: 'show.php?catid=1' in the url will create a variable
'$catid' with the value of '1' in the script show.php.

if you then want that variable to be available in a function
you can pass it as an argument

ie:
a_function($catid)
{
#function stuff
}

or you can call the variable into the function via the special
global call

ie:
b_function()
{
global $catid;
#function stuff
}

read up on function arguments adn function scope, it's all
in the php-documentation available on
once youve read the basics (took me 3 hours) go and have a look
at somwone elses code. see what they have done and learn
from that, or get a book for advanced stuff.

skottieb:)
 
The latest release of PHP (4.3.0 ?) has "register_globals" turned OFF by default. This is a change to earlier versions where it was on and you could simply use the variable in the script that you passed it to. It is possible to turn it on, but there is a good reason(so they say) for leaving it off.(see the end of this post).

eric2003, since you are new to PHP I worry about turning "register_globals" on and use the method outlined by Bastien. It is a better programming practice anyway.
No offence to skottieb. ;-)
I've just had to fixa load of stuff I wrote when I first started with PHP because of this very issue.

In short, think of it like this.
When you pass a variable to a new script via the url:

[red]mypage.php?myVar=1[/red]

The info passed int he URL is stored in an array (called a superGlobal) called $_GET.
You then have to get mypage.php to RETRIEVE the value for myVar, it isn't available to your script automatically.

So at the top of mypage.php you can set a new variable with the value for myVar with

[red]$myNewVar = $_GET['myVar'];[/red]

(note the lack of a $ and the quotes around myVar)

Why use a different name for the variable?
Well, you don't have to. But the reason the PHP team have done this is to improve security. It means that someone has to guess the name of a variable used in your script if they are going to try and hack it in some way. The variable in the URL isn't the variable that is actually used ;-)

Hope that helps and I didn't confuse you even more!
 
Yes, I believe the newest version of PHP does have register_globals off. I ran into this when I reloaded Windows last week. All my php scripts with $_GET worked prior to that. I downloaded/installed php from the web, and then they did not. What was installed was a slightly newer build. The workaround, as noted above, is to simply declare a variable for the value of the $_GET array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top