Jump to content

Few Questions


Lamez

Recommended Posts

1.Variables, could I do this:

 

<?php

$var = "cat";

echo "I love $var";

$var ="dog";

echo "I love $var";

?>

and the output will be

I love cat

I love dog

 

2.How do I do this

page.php?action=members&user

or

page.php?action=login&pass&user

 

or somting like that

Link to comment
Share on other sites

1. Yes. Try it out. Every time you run the assignment operator on a variable, you effectively overwrite the previous value.

 

2. First off, to retrieve information from the URL, you need to use the $_GET global. This is an array that is set up in PHP to automatically return any parameters passed via the URL. However, based on your sample, I would definitely encourage you to never pass user information such as password, SSN, etc through the URL. Always use POST (and preferably a secure server for the SSN and any other personal info). Check out this page of the PHP manual for details on the predefined variables including $_GET.

Link to comment
Share on other sites

I know that, but how do I do this

 

?action=page&somthing

 

In and of itself, that sets the variable something but doesn't assign a value to it, so it's pretty much worthless. If you are attempting to assign multiple values to a single variable, you may want to do something like this:

 

URL: index.php?cat=1,2,3,4

<?php
$cats = explode(',', $_GET['cat']);
?>

Link to comment
Share on other sites

Arrays are great for storing lists of similar things.

 

eg colours are similar, with just different names:

<?php
$colours = array("Red","Orange","Yellow","Green","Blue","Indigo","Violet");

print "In the rainbow the colours are:";
foreach ($colours as $key => $value)
{
  print "<li>$value</li>";
}
?>

 

Obviously not too useful, but what if you make an array with all the countries in the world, or states in america, or maybe months in a year. How about car manufacturers?

 

If someone wants to sign up to your site, you can have a drop-down box with a list of all the countries in the world, using a similar loop to the one above. Or you could have the same for car manufacturers.

 

Say you made a website for a garage, and wanted a set of checkboxes

<?php
$extras = array("pas"=>"Power Assisted Steering","alloys"=>"Alloy Wheels","CD"=>"CD player","SUN"=>"Sun Roof");

print "Which extras do you require?";
foreach($extras as $key => $value)
{
print "<input type=\"checkbox\" name=\"extras\" value=\"$key\" />$value<br />";
}
?>

 

Would produce

 

Which extras do you require?

[ ] Power Assisted Steering

[ ] Alloys

[ ] CD Player

[ ] Sun Roof

 

And if there were 50 extras, it would save you a fair bit of typing making all those input fields.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.