How do I insert PHP into an HTML page?
PHP and HTML are completely distinct languages. You need to help your server tell them apart so that it knows which parts to process as PHP. Always open PHP code with <?php and end it with ?>. You can do this as many times as you want in your document.
Everything between <?php and ?> must use correct PHP syntax, or else you’ll get one of those dreaded “parse errors” that can be very difficult to figure out when you’re new to PHP. So let’s talk a little about correct syntax.
First, let me explain how to insert comments. You know that something like <!-- comment --> in an HTML document gets ignored because it’s a comment. There are few ways to do the same thing in PHP. Here are the two you’ll see used in themes most frequently, the // syntax and the /* ... */ syntax:
<?php
$myVar1 = 'How are you?'; // two slashes indicate that everything until the end of the line is a comment
$myVar2 = 'Well, you?'; /* if your comment spans more than one line,
like this one, use the slash-asterisk format. */
$myVar3 = 'Well.';
?>When editing themes (or plugins), you’ll encounter variables, functions, and statements. Each has its own syntax, so we’ll spend a section on each.
9 comments »
Can this be used to display a different chunk of sidebar content depending on what page number you are on?
Yes, PHP can be used for that. See conditional tags.
I know I need to add the PHP file to the same directory as the page containing the PHP script but which FTP directory does Wordpress store the posts in pls?
@gadget: For general WP support, please use the WP support forums. The short answer: WP uses MySQL, not PHP, to store data.
Hello,
If the directory I am accessing from the site contains both index.php and index.htm ,What happens?
That’s not really a PHP question. It depends on your server configuration. But why ask? Just try it and see what happens.
Hello,
If I use single quote to assign a string to a variable in the PHP,Can I define another variable with a double quote? I mean, can I mix and match single and double quotes according to my convince in the same PHP snippet or should I stick to only one type of delimiters throughout the snippet?
Thanks a lot for the quick reply.
Hello,
Nice tutorial! simple,easy, neat, to the point.And the best part is it inspired me to learn a new web language.Thanks a lot
Thanks. Regarding your question about quotes, you can switch back and forth all you want. But again… why ask? Just try it and see what happens. That’s the way I learned PHP.
Leave a comment