...

View Full Version : Some Basic Questions: Some kind heart, please answer.



blankskull
06-29-2010, 08:37 PM
When I try hard but I cannot understand any code, I feel like crying. Can anybody help me?




if (isset($_POST['password']) && $_POST['name'] != "")



1. Why is the sign (!) at the end? But usually I noticed that the (!) sign comes at the beginning of the $_post []. Something like if(!isset($_post......))

2. What does the "" sing mean?

3. Can I translate the code as following: if the password and name variables are not set.............

4. Is it necessary to check whether the $_post['submit'] is set or not before checking whether other variables are set or not?



<input name="check" type="checkbox" id="change_email" value="change_email" />



1. what is this input?

2. what is this name="Check"? Is it the name of the field in the table?

3. what is value="change_email"? Is it the data to be stored in the field of a table?



Another code from another PHP Coder:

if(isset($_POST['send']))
{
switch($_POST['radio'])
{
case 'change_email':
$ref = ($total - 1);
break;
case 'Optout':
$ref = $total - 2;
break;
case 'Remove_Completely':
$ref = ($total - 3);
break;
case 'No_changes':
$ref = '';
break;
default:
$ref = '';
break;
}

1. The coder has already checked whether the submit button has been pushed or not. But why did not he check whether the $_post['radio'] is set or not?

2. Can I assume that if the submit button is pushed, $_post['submit'] variable is set? In the same manner, are the variables like $_post['name'], $_post['age'] etc. set, once the user fills up the name and age fields? Or are they set, only the user pushes the submit button?

Keleth
06-29-2010, 08:53 PM
if (isset($_POST['password']) && $_POST['name'] != "")

1. Why is the sign (!) at the end? But usually I noticed that the (!) sign comes at the beginning of the $_post []. Something like if(!isset($_post......))

! at the beginning of a logic statement means to invert (true becomes false, false becomes true). When you see != it means "not equal".


2. What does the "" sing mean?

Sign*

Its just comparing the content of the variable ($_POST['name']) to a empty string.


3. Can I translate the code as following: if the password and name variables are not set.............

No. It is "If the post password is set and the post name is not an empty string..."


4. Is it necessary to check whether the $_post['submit'] is set or not before checking whether other variables are set or not?

Its usually a good idea, for security reasons. Plus, sometimes you'll have multiple buttons on a form and each might interact differently with the data.



<input name="check" type="checkbox" id="change_email" value="change_email" />


1. what is this input?
A checkbox


2. what is this name="Check"? Is it the name of the field in the table?
No, name is how a input is referenced. So if this was being sent by POST, when processed, to read it, you would check $_POST['check'].


3. what is value="change_email"? Is it the data to be stored in the field of a table?
Well, not directly no. Its simply the data being passed along the form. Although for checkboxes, many browsers don't send value, just on/off, true/false, etc.




if(isset($_POST['send']))
{
switch($_POST['radio'])
{
case 'change_email':
$ref = ($total - 1);
break;
case 'Optout':
$ref = $total - 2;
break;
case 'Remove_Completely':
$ref = ($total - 3);
break;
case 'No_changes':
$ref = '';
break;
default:
$ref = '';
break;
}


First, when asking for help, helps to actually post in a way the helper can accurately read so we can help without our eyes bleeding. Bold is not a good option, but as its written in a few places, the php or code tag is... it actually allows whitespace indent.


1. The coder has already checked whether the submit button has been pushed or not. But why did not he check whether the $_post['radio'] is set or not?
That's what the case statement is doing. If the value doesn't match any of cases given, it goes to the default case, setting $ref to an empty string.


2. Can I assume that if the submit button is pushed, $_post['submit'] variable is set? In the same manner, are the variables like $_post['name'], $_post['age'] etc. set, once the user fills up the name and age fields? Or are they set, only the user pushes the submit button?
Yes, when a submit button is pressed, the $_POST variable with the NAME of the submit button is set (not a variable named submit). What its set to depends on the browser in question.

As to the second part, its important to note web programming vs software programming... a webpage when loaded is COMPLETELY on the users end, processing is done on the server end (now, there are quibbles such as JS or AJAX, but in the end, its still user data being sent back to the server for processing). The question of if a variable is set when a user enters data into a field is actually moot: there is no variable. When a user hits a submit button, data is now sent to a server, where it is processed into variables. So as a literal answer, the variables are set when the button is hit, but that's only because they are not variables on the users end.

hernantz
06-29-2010, 09:00 PM
1. the (!) sign means no. Is a logic operator.
for example, in your case

if (isset($_POST['password']) && $_POST['name'] != "")
means that $_POST]['name'] must be diferent (!=) to nothing ("") to pass as a condition together with the isset($_POST['password']).
2. the "" means empty, it can also be '' (simple quotes)
3. Yes you can:
if ( !isset($_POST['password']) && !isset($_POST['name']))
4. Yes you should, check if the form was submitted first, and then validate the form inputs (if they are empty, if they have the expected data, etc)
If the form was not submited, you may redirect the user to the page that holds the form.


But why did not he check whether the $_post['radio'] is set or not? maybe it is marked as default (usually the first radio) in the form, but its not always like this, in that case, it is assumed that one radio will remain checked when pushing the send button.

But in case of the name, password, etc fields, if the user push the send button, they will remain empty.

Keleth
06-29-2010, 09:51 PM
3. Yes you can:
if ( !isset($_POST['password']) && !isset($_POST['name']))

That is not the same as the code given


if ( !isset($_POST['password']) && $_POST['name'] != "")

They do not read the same way.

blankskull
06-30-2010, 04:03 AM
You people are really kind and helpful. Thanks to all of you. Thank you, Keleth.

hernantz
06-30-2010, 02:54 PM
That is not the same as the code given


if ( !isset($_POST['password']) && $_POST['name'] != "")

They do not read the same way.

I know, I thought he wanted to to get "if the password and name variables are not set"

blankskull
06-30-2010, 06:07 PM
1. $refrence_user==' '
2. $Refrence_User=" "


Dear Keleth and Hernantz, Again I have some more questions:

1. Are options 1 and 2 the same? I guess that option 2 means that the value of $reference_user variable is set to blank.

2. But what is the meaning of option 1? Also I guess that the meaning of option 1 is that the value of $reference_user is equal to blank or null. Am I right?

Again:

1. $reference_user==' '
2. $reference_user==" "

Do the above mentioned options mean the same?

Keleth
06-30-2010, 06:23 PM
$refrence_user == ' '

means to check if $refrence_user is set to a space.


$Refrence_User = " "

means to set $Reference_User to a space.

Note that they are two difference variables... variables are case sensitive. You changed it in the bottom, but its different (top from bot).

Beagle
06-30-2010, 09:44 PM
Another way of saying this:


$variable == ""

is an expression that evaluates to a boolean by testing if both operands are equal. So (1 == 1) evaluates to true, and (2 == 1) evaluates to false. That's why you see these in conditional statements:


if (1 == 1) /*do something*/ else /*do other thing*/

So if the expression evaluates to true, the first code block is entered and executed. If the expression evaluates to false, the else block of code is entered and executed.

Meanwhile:


$variable = "";

is an assignment. It assigns the value on the right to the location on the left. Like putting your groceries into a bag. You can't do:


1 = $variable;

Because 1 is not a location, it's a value. It's like trying to put the bag into your groceries.

blankskull
07-02-2010, 07:17 AM
It's like trying to put the bag into your groceries.
Thanks, Beagle, for translating the code in a man's daily life English. It's easy to understand.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum