Jump to content

Recommended Posts

Thanks C.V
I tried turning this block of code into PHP from HTML because I needed it to be a [color=green]case[/color] in a [color=green]switch[/color]
im having a hard time converting it from html to php. I fix one line and the next give me a parse error.
the times you are requiered to use piticular brackets and quotes in this language ( or any other is
throwing me off. I think the first line is okay but the rest I could really use some help.
echo "<form method='post' action='$PHP_SELF}'>";
echo "<input type=hidden name=edit value='$_POST['edit']'>";
echo "<input type="hidden" name="id" value="{$_GET['id']}">";
echo "<input type="radio" name="newName" value="yes"> Yes <br />";
echo "<input type="radio" name="newName" value="no"> No <br />";
echo "<input type="submit" name="submit" value="Submit" />";
Link to comment
https://forums.phpfreaks.com/topic/26025-forms-in-php/#findComment-119010
Share on other sites

okay here is a quick and dirty rundown of using quotes. You have 2 quote options: single quotes and double quotes.  If you are displaying plain old text and nothing else, for all intents and purposes, they do the same thing.  The real difference comes in wanting to throw variables into it or use quotes inside of quotes. 

For instance, you can do this:

echo "blah 'blah' blah";

but not this:

echo "blah "blah" blah";

because the parser will think you are closing the brackets.  You can avoid this by doing this:

echo "blah \"blah\" blah";

using the backslash is called escaping the character.  It tells the parser to parse it as regular text and not some key thing.  The escape character also has other purposes too, like doing this:

echo "blah \r\n blah";

this is (not the same) but for the most part, the same as doing this:

[code]echo "blah <br> blah";[/code]

Okay back to the quotes.  When you have a simple variable, if you do this:

$blah1 = "bob";
$blah2 = 'joe';
echo "hello $blah1 and $blah2"; // notice the double quotes

you get this:

hello bob and joe

as mentioned above, with simple text, for all intents and purposes, there is no difference between $blah1 and $blah2. but if you did this:

echo 'hello $blah1 and $blah2'; // notice the single quotes

the parser will look at your variables and treat them like normal text, instead of replacing the variables with their values.  it will literally output this:

hello $blah1 and $blah2

okay so now you know if you wanna do hello bob instead of hello $blah1 you need to use double quotes.  okay moving along to more complex variables, like arrays:

$blah1 = array('foobar' => 123);

okay I can do this:

echo $blah1['foobar']; 

and it outputs this:

123

but if I want to do this:

echo "$blah1['foobar']";double quotes

or even this

echo '$blah1['foobar']'; //single quotes

the parser gets confused, as to whwhere the string starts and ends, what part is the variable and what part is the string, and a whole other mess of things.  Even if you used the nifty \ character, there would still be confusion as to what part is the variable and what part is some other something else.  This is called ambiguity - that is, not being able to tell what from what.  In order to avoid this, we have { } so in order for php to parse it right, it would look like this:

echo "I can count to {$blah['foobar']} !";

outputs:

I can count to 123 !

here is what your code should look like:

[code]
echo "<form method='post' action='{$_SERVER['PHP_SELF}'>";
echo "<input type='hidden' name='edit' value='{$_POST['edit']}'>";
echo "<input type='hidden' name='id' value="{$_GET['id']}'>";
echo "<input type='radio' name='newName' value='yes'> Yes";
echo "<input type='radio' name='newName' value='no'> No";
echo "<input type='submit' name='submit' value='Submit' />";
[/code]

okay now, not to confuse you even more, but there are even more efficient ways of doing this, like using the [url=http://www.php.net/heredoc]heredoc[/url] method, but for now, this will work for you.
Link to comment
https://forums.phpfreaks.com/topic/26025-forms-in-php/#findComment-119015
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.