Page 1 of 2

php string variables embedded in HTML

Posted: Sun Dec 08, 2002 10:48 pm
by permutations
I am very new to PHP--please excuse the newbie-ness of my questions. I'm puzzled by a small code snippet that I got from a book:

===========
<form action="you_are.php3">
Please enter your name:<br>
I am...
<?php
print('<input type="text" name="person" value="' . $person . '" size="15">');
?>
<input type="submit" value="Go!" size="15">
============

This little snippet runs just fine on two Unix servers I have access to--one running PHP3 and one running PHP4. But on my own PHP server (running with PWS 5 on Win2000), I get an error message for the print line. It says the variable "person" is not defined. This is the same version of PHP running on one of the Unix servers I tried (4.2.3).

Two questions:

(1) Why must "$person" be surrounded by " . " for this statement to work? Why must two spaces be concatenated on either side??

(2) Why doesn't this silly thing run on my own PHP interpreter (which it only took me 16 hours to successfully set up)?

This small details are significant for me because they affect some other code I'm writing. Thanks in advance for your help.

Posted: Mon Dec 09, 2002 12:05 am
by dusty
concatenation is the word that describes the use of the periods. it's joining 2 strings together. the reason you are getting the error undefined variable is due to different error handling settings for the 2 different servers. it's not a critical error.

still confused...

Posted: Mon Dec 09, 2002 12:19 am
by permutations
I'm still puzzled about the "value" assignment in this line:

print('<input type="text" name=person value="' . $person . '" size="15">');

(1) I did some experiments on the remote PHP3 server. If the $person variable doesn't have spaces concatenated on either side, it generates an error. WHY??

(2) Exactly which error handling parameter is set incorrectly on my PHP server? I'd like to fix it.

and another question, as long as I'm at it...

(3) Why does the "each" statement have such weird return values? I don't get the point of the redundancy.

Thanks in advance...

Posted: Mon Dec 09, 2002 12:37 am
by dusty
i'm not sure about the error in (1). spaces aren't really needed, just makes it easier to read.

you don't have incorrect settings, they're just different between the 2 servers. you can read more at http://www.php.net/error_reporting

not really clear about your problem in (3)

inching closer

Posted: Mon Dec 09, 2002 12:56 am
by permutations
Actually, the spaces are needed. I tried it. If they're not there, it doesn't work right. You see the variable name instead of the contents.

I think my problem is not error reporting, but that somehow my setup of PHP is requiring that variables be defined before they're used. I'd like to turn that off, but I don't know how.

Thanks for your help.

Posted: Mon Dec 09, 2002 1:06 am
by dusty
just because it doesn't work for you, doesn't mean they ARE required.
echo "hello" . "world"; would work the same as echo "hello"."world"; with no errors.


did you read anything on the error_reporting site?
if you want to make error_reporting set it in php.ini. there is an example in php.ini if i remember correctly that shows the correct setting for displaying all but E_NOTICE

my screwed up settings

Posted: Mon Dec 09, 2002 1:23 am
by permutations
I still don't understand the space concatenation thing. It really did not work when I tried it without. I can't explain why and I'd like to know.

The one thing I do know is that the settings on my PHP setup are so screwed up that I can't run anything. For now I'm testing on my ISP's site. It will surely take me some time to figure out how to fix this. If anyone knows exactly which settings are causing my constant parsing errors, please let me know. I didn't change any of the default settings except for directory pointing.

Posted: Mon Dec 09, 2002 3:11 am
by horgh
why don't you try

Code: Select all

print "<input type="text" name="person" value="$person" size="15">";
that should work.
bwt. i tried your version with the spaces and w/o them and it worked too.
perhaps you should paste the error message here for better understanding your problem...

error message

Posted: Mon Dec 09, 2002 4:28 pm
by permutations
Your version works perfectly. If I do it the way I originally posted without the spaces, I get this error message:

Parse error: parse error in /home/s/c/scr/www.scr.com/who_are_you.php3 on line 12
<input type="text" name=person value="

(I changed the directory and domain names.)

What is the purpose of escaping the quotes? I don't really understand what escaping characters is for or what it does.

newline characters don't work

Posted: Mon Dec 09, 2002 4:43 pm
by permutations
Related to this is another problem... I try to use \n to insert a newline character so I can read my debug output more easily, but it's always ignored. Even when I type examples out of a book, it's ignored. Any ideas?

Posted: Mon Dec 09, 2002 4:49 pm
by volka
take this single line of code

Code: Select all

$string1 = "just a "test"-string without the "-char escaped"; $i=1;
how should the parser know what's in- and what's outside the stringliteral?
You have to escape the special meaning of the char that will mark the end of the literal

Code: Select all

$string1 = "just a "test"-string  " is escaped properly"; $i=1;
and now the colors of the syntaxhighlighter here make more sense ;)

\n is ignored by html-browsers if not within a <pre>...</pre>-element (preformatted). add a <br/> where you want the line break.

THANK YOU!!

Posted: Tue Dec 10, 2002 12:28 am
by permutations
Volka--Thank you! I'm starting to see the light. You clearly explained a number of things that have been puzzling me. I especially liked your wording of what "escape" is for--"escapes" the special meaning of the character. Now it makes sense! And yes, the color coding in the IDE makes sense now, too--I've been wondering about that!

I have another question for you... What is the difference between a single quote and a double quote when quoting strings? I sort of try things randomly until it works, but that's not a good method. :wink:

Posted: Tue Dec 10, 2002 1:46 am
by volka
I couldn't explain it better than http://www.php.net/manual/en/language.types.string.php ;)
try this one to see the difference

Code: Select all

$vari = 'value';
echo '--- $vari ---';
echo "--- $vari ---";
echo "--- $variable ---";
echo "--- {$vari}able ---";

Posted: Tue Dec 10, 2002 6:44 pm
by permutations
Thank you for the manual link and the examples. I understand now!

I'd like to ask you about a very simple little bit of code I have that mysteriously doesn't work:

Code: Select all

<?php
function rw_file () {
	$fp = fopen("pictures.txt", "r+");
	if (!$fp) {
		print "File open failed";
		return false;
	}
	while ($data = fgetcsv ($fp, 600, "~")); {
		print "var_dump of data: ";
		var_dump ($data);
		print "<br>";
	}
	fclose ($fp);
	return true;
}
rw_file ();
?>
This reads a teeny weeny little text file, just 633 bytes, with two records where each value is delimited with "~". The return from fgetcsv is always null. It's driving me nuts because I can't see anything wrong with it. No matter what I try, it comes up null. I can read the file with fread(), but not fgetcsv(). What am I doing wrong?

string variables again

Posted: Wed Dec 11, 2002 1:22 am
by permutations
I realized that I still have my original question. I found the exact line that works in a way I don't understand. I didn't have the exact line before.

This line works:

Code: Select all

print('<a href="who_are_you.php3?person=' . urlencode($person) . '">Back to Who Are You?</a>');
Notice that urlencode($person) is oddly surrounded by a single quote, a space, a concatenation symbol, and another space. If I leave out the spaces and the concatenation symbol:

Code: Select all

print('<a href="who_are_you.php3?person='urlencode($person)'">Back to Who Are You?</a>');
then I get this error message:
  • Parse error: parse error, unexpected T_STRING in D:\Inetpub\wwwroot\phptests\TMPxmff6y1cm.php3 on line 9
Could you explain why the first one works and the second one doesn't? I don't understand. Thanks in advance.