Page 1 of 1

[Solved] Basic PHP questions

Posted: Fri Dec 10, 2004 10:38 pm
by Hibba
I have a few simple questions for those who really know PHP.
-------------
Question 1:
First of all, I am assigning html to variables. Currently, I have done this:

Code: Select all

<?php
$question1 = include('question1.php');

//where question1.php looks like...

$one = '
<p>A question?</p>
		<p><input type="text" name="name" size="10"></p>
';
return($one);
?>
My reason for this is to avoid escaping the quotes. Might there be a better way or am I not thinking correctly?
-------------
Question 2:
In PHP, when you use include(), does it always have to be a PHP file, or can you include an HTML file?
-------------
Question 3:
When you assign HTML to variables similiar to this echo:

Code: Select all

<?php
echo'
	<html><body>
	
	<form method="post" action="hello_name.php">
	
	Enter Your Name
	<input type="text" name="UserName"></input><br>
	
	<input type="submit" name="submit" value="click"></input>
	
	</form>
	
	</body></html>
	';
?>
How do you make the action PHP_Self?


That is all for now. I am sure it will take someone who knows PHP all of 1 minute to answer these questions, but I can't seem to figure it out through the manual and other forums...

THANKS!

Re: Basic PHP questions

Posted: Fri Dec 10, 2004 11:11 pm
by genetix
-------------
Question 1:
You shouldn't need to "escape" any quotes. That script should work fine since the text from the input box is separate from the webpage.
-------------
Question 2:
It can be an HTML file. Such as an HTML header file. Any page that has include on it must be a .php or similar type.
-------------
Question 3:
The easyest way would be to scape the echo temporarily.

Code: Select all

<?php
$one = '
<form action='. ANY PHP .' method="post">
<p>A question?</p>
<p><input type="text" name="name" size="10"></p>
';
?>

Hope that helps.

Posted: Fri Dec 10, 2004 11:16 pm
by Benjamin
In my opinion your method is fine for question 1, although I am not sure why you have the return($one) in there. If you want to print it just just echo $one;.

For Question number 3, I would probably code it like this...

Code: Select all

<?php
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter Your Name<input type="text" name="UserName"></input><br>
<input type="submit" name="submit" value="click"></input>
</form>
</body>
</html>
<?php
?>

Posted: Fri Dec 10, 2004 11:46 pm
by Hibba
Thanks so much, this does help!

What exactly does this mean??
Any page that has include on it must be a .php or similar type.

Posted: Fri Dec 10, 2004 11:50 pm
by Benjamin
What he means is that you cannot put an include statement inside of an html file because the html file isn't parsed by the php interpreter. You can include any file type.

Posted: Sat Dec 11, 2004 12:20 am
by Hibba
So you mean a nested include within a .html file that I am already including in a .php file?

Posted: Sat Dec 11, 2004 8:17 pm
by Bennettman
PHP include can be thought of as copy&pasting the included file into the main file at that point. If you have a PHP file including an HTML file, the HTML file can have PHP code and it will be parsed properly. That includes an include within the HTML file, yes.

On the flip side, .html pages don't protect server-side code from being read like .php does (i.e. if someone goes straight to the HTML page, they can see the PHP code), so it's advisable to have all PHP code in .php files.

However, the point is, you can include any file type you want. If it has PHP code in, the PHP will be parsed. Any non-PHP code will be output. Include an MP3 for example and you'll get a few megs of gobbledegook on the page.


A very useful assignment tool that'd work well for you, is this (for Q1):

Code: Select all

<?php

$one = <<<HTML
<p>A question?</p>
        <p><input type="text" name="name" size="10"></p>
HTML;

?>
Basically, instead of starting a string, put three < digits and any word you like (I use the type of content, in this case HTML), then you can put in the content from the next line without worrying about escaping slashes and using variables etc. Variables will work - arrays and functions won't. End the assignment with a new line containing the word you used and a semicolon (these two must be on their own line).

This also ties into Q3:

Code: Select all

<?php

print <<<HTML
    <html><body>
    
    <form method="post" action="$PHP_SELF">
    
    Enter Your Name
    <input type="text" name="UserName"></input><br>
    
    <input type="submit" name="submit" value="click"></input>
    
    </form>
    
    </body></html>
HTML;

?>
And, to add to AGTLewis' example

Code: Select all

<?php echo $_SERVER['PHP_SELF']; ?>
is the same as
<?=$PHP_SELF?>
if your hosting has register_globals and short_open_tags switched on
They're both methods of calling a variable from outside PHP - the top one is longer but will work in any configuration.

Posted: Sat Dec 11, 2004 10:11 pm
by Hibba
BUT if I have something like:
$question1 = include('question1.html');

It will output an additional "1" in the form...

What's a fix?

Posted: Sat Dec 11, 2004 10:32 pm
by Bennettman
If the only reason you're doing the including is to avoid escaping quotes, then there's no point, your solution is in my post above ;p

I don't see the point anyhow, but basically the "1" is the result of the include command - it's just saying "it worked". The proper way to fix it is to have the value in a variable in the included file, and have a "return $var" command at the end of that file. Which makes including the file trying to avoid having to assign it pointless.

However, if you still want to go the inclusion route, you could take a look at the file, fopen etc functions, which read text directly from the specified file.