Test

Powered by Blogger.

php hello world tutorial



On some servers, ASP style tags have been enabled. They look like this:

<% [code here] %>

Since this is not necessarily supported on all servers, you may wish to use the full version instead.

The same goes for the special output version, which looks like this:

<?= [output here] ?>

The last one is the same as writing:

<?php echo "some output"; ?>

As you can see, you do save some keystrokes, and if you're writing code for your self only, you may be tempted to use the short versions instead. Just be aware that if you ever change to another server, the last two options may be disabled.
In and out of PHP
When writing webpages, you naturally use HTML all over the place. You will quickly need to combine HTML tags with output generated from PHP. Now that's easy:

<?php
$myVar = "Hello world!";
echo "<b>" . $myVar . "</b>";
?>



We simply create a string consisting of three parts: The starting bold tag, our variable, and then the ending bold tag. Using the dot operator, which concatenates strings in PHP, we output the entire thing as if it was just another piece of text. In simple cases like this one, our above example is just fine. However, if you suddenly start having way more plain HTML, with a minimum of PHP code required in it, the above approach is actually not the best way to go. You may do it like this instead:

<?php
$myVar = "Hello world!";
?>
<i>We have HTML <u>all</u> over the place here!</i><br />
<i>But some PHP as well, as you can see:</i><br />
A message from PHP: <b><?php echo $myVar; ?></b>

In this tiny example, we have way more plain HTML and text than we have PHP code. It makes sense to jump out of PHP in those cases, and according to the PHP manual, it even offers better performance than outputting huge amounts of HTML through PHP. So we can jump out of it, but can we jump in again? Of course. And PHP is actually quite flexible in this area, as the following example will show you:

<?php
$myVar = 42;
if($myVar == 42)
{
    ?>
    <b>Yes, the number is in fact 42!</b>   
    <?php
}
?>

As you can see, we jump out of PHP in the middle of a conditional block of code. PHP understands this just fine. The HTML and text part is only outputted if $myVar is actually set to 42. Try changing it and you will see that PHP respects the condition even when you mix in plain HTML and text.
Next 

No comments:

Post a Comment

RSS

Categories

Followers

Blog Archive

php hello world tutorial



On some servers, ASP style tags have been enabled. They look like this:

<% [code here] %>

Since this is not necessarily supported on all servers, you may wish to use the full version instead.

The same goes for the special output version, which looks like this:

<?= [output here] ?>

The last one is the same as writing:

<?php echo "some output"; ?>

As you can see, you do save some keystrokes, and if you're writing code for your self only, you may be tempted to use the short versions instead. Just be aware that if you ever change to another server, the last two options may be disabled.
In and out of PHP
When writing webpages, you naturally use HTML all over the place. You will quickly need to combine HTML tags with output generated from PHP. Now that's easy:

<?php
$myVar = "Hello world!";
echo "<b>" . $myVar . "</b>";
?>



We simply create a string consisting of three parts: The starting bold tag, our variable, and then the ending bold tag. Using the dot operator, which concatenates strings in PHP, we output the entire thing as if it was just another piece of text. In simple cases like this one, our above example is just fine. However, if you suddenly start having way more plain HTML, with a minimum of PHP code required in it, the above approach is actually not the best way to go. You may do it like this instead:

<?php
$myVar = "Hello world!";
?>
<i>We have HTML <u>all</u> over the place here!</i><br />
<i>But some PHP as well, as you can see:</i><br />
A message from PHP: <b><?php echo $myVar; ?></b>

In this tiny example, we have way more plain HTML and text than we have PHP code. It makes sense to jump out of PHP in those cases, and according to the PHP manual, it even offers better performance than outputting huge amounts of HTML through PHP. So we can jump out of it, but can we jump in again? Of course. And PHP is actually quite flexible in this area, as the following example will show you:

<?php
$myVar = 42;
if($myVar == 42)
{
    ?>
    <b>Yes, the number is in fact 42!</b>   
    <?php
}
?>

As you can see, we jump out of PHP in the middle of a conditional block of code. PHP understands this just fine. The HTML and text part is only outputted if $myVar is actually set to 42. Try changing it and you will see that PHP respects the condition even when you mix in plain HTML and text.
Next 

No comments:

Post a Comment