I have a real beginner question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Innovative_Andy
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 9:40 am

I have a real beginner question...

Post by Innovative_Andy »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey everyone, 

I just started learning PHP and there is some stuff I really am confused on. 

I know a little code but nothing fancy. 

But Can somebody explain to me what this is doing?

Code: Select all

$act = false;
	if($_REQUEST['act'])
		$act = $_REQUEST['act'];
	if(!$act)
		$act = 'form';
I'm guessing this is a loop statement and i've looked on PHP.Net and all these other forums and google and Wikipedia and everything. I still have not found anything. I don't even know what the tags mean...

$act?
$_REQUEST?

Code: Select all

$act = false;
	if($_REQUEST['act'])
		$act = $_REQUEST['act'];
	if(!$act)
		$act = 'form';
???
I'm so lost...

Please forgive me if this is covered somewhere else.

Any help will be greatly appriciated.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]2.[/b] Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.[/quote]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Here's what's happening in plain english:

Set $act to false. Check if $_REQUEST['act'] is non-zero, if so, set $act to it. Now check if $act is not non-zero, if so, set $act to the string "form."
Innovative_Andy
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 9:40 am

Post by Innovative_Andy »

(I apologize about not following the forum rules. I'm just really confused and have a huge headache from this.)

Thank you for the help. But one more thing.

What exactly is:

"$act"?

"$_Request"?

"['act']"?

Are they just the names of each string? If so, what are they to be representing?

[Once again, If i didn't follow the "tag" rules. I apologize. Like I said I'm just starting this whole php thing and i'm not sure what i'm to do with what.]

I honestly appriciate the help so far.

Thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$act is a variable name. Similarly, $_REQUEST is another variable name, but this is a superglobal.

['act'] is a look up into an array (the brackets are.) In this case, it's looking for the index "act" in the array $_REQUEST.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

Innovative_Andy wrote:What exactly is:

"$act"?

"$_Request"?

"['act']"?
$act is a variable.
$_REQUEST is essentially superglobals ($_GET and $_POST etc, look them up on PHP's website for more info).
['act'] is refering to the name of the variable in the $_REQUEST array. For example:

Code: Select all

http://www.example.com/index.php?foo=1&bar=0

Code: Select all

echo($_REQUEST['foo']); // Outputs: 1
echo($_REQUEST['bar']); //Outputs: 0
Last edited by someberry on Wed May 31, 2006 10:14 am, edited 1 time in total.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here is your code with come comments

Code: Select all

<?php
// Set variable 'act' to a default of zero (false)
$act = false;

// If a form field called 'act' was posted OR 
// a querystring variable called 'act' was sent AND
// 'act' is not zero, change the default value of the 
// variable $act to the value of the passed var 'act'
    if($_REQUEST['act']) 
            $act = $_REQUEST['act'];

// Now, $act is going to be either 0 or some other 
// value that was passed by a form or querystring
// Check to see if $act is zero (false), if it is 0 
// (false) change the default value of the variable 
// $act to the string 'form'
    if(!$act)
            $act = 'form';
?>
Innovative_Andy
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 9:40 am

Post by Innovative_Andy »

Alright, so then the code would be saying:

"$act" is going to be false.

as in, going to be set to false as in "not true" or just named "False"?

"$_Request" is a variable name (just like $act) but this is a superglobal so it represents every variable available in the global scope of the script?

if that is the case, Wouldn't there need to be some sort of reference to understand what the variable is? or is it what the "user" will input into the database?

['act'] is the name of the variable in the array for $_request.

so then the code is

[If I didn't do this right i apologise]

Code: Select all

$act = false;
	if($_REQUEST['act'])
		$act = $_REQUEST['act'];
	if(!$act)
		$act = 'form';

where is says "if(!$act)" i'm assuming that means "IF ['act'] is NOT false, then ['act'] is 'Form'."

Is that right?


You all are so great and I appriciate the help so much. I'll be coming to this forum much more often than I did. I just started a new job and they offered me a coding position and it is mainly PHP and for some reason, they don't feel like having someone teach/help me would be somewhat beneficial. So I am teaching myself and i'm making my own little projects so I can learn and using other snips of code as reference. so that Is why i'm confused. But you all are so helpful so thank you very much.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Innovative_Andy wrote:Alright, so then the code would be saying:
"$act" is going to be false.
as in, going to be set to false as in "not true" or just named "False"?
False as in a representation of 0. It is not anything. This is a 'defaulting' mechanism added by the developer so that if the if checks fail, it still has a value.
Innovative_Andy wrote:"$_Request" is a variable name (just like $act) but this is a superglobal so it represents every variable available in the global scope of the script?

if that is the case, Wouldn't there need to be some sort of reference to understand what the variable is? or is it what the "user" will input into the database?
$_REQUEST is an array. It is a combination of $_GET (values passed by the url ala 'somepage.php?var=value') and $_POST (values passed by a form submission). Items in the $_REQUEST superglobal array are usually sent by A) a user entering data into a form or B) a user clicking alink in which the developer added in querystring vars for information grabbing later.
Innovative_Andy wrote:['act'] is the name of the variable in the array for $_request.
In this case, act is the name of the index in the $_REQUEST superglobal array. If there were 28 items in the $_REQUEST array, the means by which you would identify this particular key/value pair is by calling the array with it's index ($_REQUEST['act']).
Innovative_Andy wrote:so then the code is

[If I didn't do this right i apologise]

Code: Select all

$act = false;
	if($_REQUEST['act'])
		$act = $_REQUEST['act'];
	if(!$act)
		$act = 'form';

where is says "if(!$act)" i'm assuming that means "IF ['act'] is NOT false, then ['act'] is 'Form'."

Is that right?
You are right.
Innovative_Andy
Forum Newbie
Posts: 5
Joined: Wed May 31, 2006 9:40 am

Post by Innovative_Andy »

Okay, that help a lot.

Thank you guys so much.

If I have anymore questions, I'll be sure to check here first.

Thanks again.
Post Reply