Jump to content

Why is variable used for


zeeshan

Recommended Posts

that script will assign the values on the right to the variables on the left.Variables are used as an id for values with a speicifc name. If you had an html form and one of the text fields had the name 'Email Address' then when people entered their email address, of course it would be different for each person. The variable you might use to assign those different values could be called $useraddr and then in the script it wouldn't matter if the email address was different, because it would always have the same variable name, so you could then use the echo() function to print the email address on the web page.

Link to comment
Share on other sites

means like echo($email) then the email provided by the member will be provide i mean will be shown on the browser like this script

function addNewUser($username, $password,$confirm password,$email){   global $conn;   $q = "INSERT INTO users VALUES ('$username', '$password','$confirm password','$email')";   return mysql_query($q,$conn);

Link to comment
Share on other sites

Hi, that script will add a new user to a mysql database. Not print the email address on the browser.the script would look more like this<?php$useraddr = $_POST['email'];echo($useraddr);?>

Link to comment
Share on other sites

Variables are used to store any data from any source (a user input, something extracted from a databse, contents of a file, etc.). They are called "variables" because they can "vary" i.e. "change" during the execution of a script. For example

<?php$variable = 1;echo $variable + 3;?>

will output "4", because "1 + 3" equals 4.Variables can also be used to form the value of another variable. For example:

<?php$variable = 1;$sum = $variable + 3;echo $sum;?>

will again output "4", because the value of the variable "$sum" was created by summing the value of $variable (1) with "3", which is again "1 + 3", equaling "4".Values from forms can be fetched by using the variables* $_GET or $_POST, depending on the "method" attribute on your HTML form. You can fetch a form field by using the form $_*method*[*field*], like for example, with a form

<form method="post" action=""><input type="text" name="myField" /> <!-- The value myField on the left --></form>
You can output the submitted value with
<?phpecho $_POST['myField'];?>
* Arrays really. But arrays are a more complex (yet important!) topic you're yet to cover.
Link to comment
Share on other sites

When some students start programming, they wonder why they should use variables at all since they could just use the number or text instead. Here's a basic example where that question might come up.

<?php $age = 45; $name = Jeff; echo $name . " is " . $age . " years old." ?>

And that really does seem like a waste when you can write: echo "Jeff is 45 years old."But let's try something different. Never mind why you might want to do this. You wouldn't. But it's similar enough to the kinds of things programmers do a lot of that it makes a good lesson.Just pretend that you wanted to print my name 100 times. Easy. Just type this:

<?phpecho "Jeff<br />";echo "Jeff<br />";echo "Jeff<br />";echo "Jeff<br />";echo "Jeff<br />";echo "Jeff<br />";etc. etc. etc.?>

You get the idea? You'd have to type the same line of code (or paste it) 100 times. What a waste! Check out the alternative:

<?php $i = 0;while ($i < 100){   echo "Jeff<br />";   $i = $i + 1;}?>

See how that works. We call it a loop. The statements inside {} will execute over and over until a certain condition is met. In this case, they will loop while $i is less than 99. Since $i started at 0 and is increased by 1 with every cycle, the loop stops after 100 cycles. And at the start of every cycle, the loop prints my name.The loop and the variable do the work for me.

Link to comment
Share on other sites

Think of a variable as a container that can hold different things such as string, integer, array, and boolean.

$weAre = 'We are '; // string$today = 2; // integer$day = array(0=>'Sunday',1=>'Monday',2=>'Tuesday',3=>'Wednesday',4=>'Thursday',5=>'Friday',6=>'Saturday'); // array (always start with 0)$access = true; // boolean

you can use this to make a little program:

if ($access == true) {	// tomorrow	$today = $today + 1; // $today is now 3 (which is Monday)} else {	// in 2 days	$today = $today + 2; // $today is now 4 (which is Tuesday)}echo $weAre . $day[$today];

Which means:if the variable $access is true show tomorrow's dayelse show in 2 days from nowbecause I set $access to true it will print:

We are Monday
This is a very simple way to see the 4 different variables in action. Of course if you take this code and paste it into your page it will always show "We are Monday" because I set $today as static so you will have to go to your site and change the variable number everyday.if you want today to be dynamic than you need to call the date function: date() and $today will become:
$today = date('w'); // integer of the day

I hope this will help you to understand the freedom that a variable can give youOf course this is just as example, there is better, faster and cleaner way of doing it :)this will help you to know about the date function and all other that come with php:http://ca.php.net/manual/en/function.date.php

Link to comment
Share on other sites

Variables are used to store data in memory. You can then perform actions with variables using functions. E.g.

print $variable; //will print the value of $variable to the page$result = mysql_query("SELECT * FROM $variable"); //Will select the table with the name of the value of $variable, and store its resource ID to another variable ($result)$file = file_get_contents($variable); //Will store the contents of file with the name of the value of $variable to another variable ($file)$field = $_POST[$variable]; //Will store the value of a posted form's field with name of the value of $variable to the variable $field

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...