PHP - Dynamic Websites 101
A Beginner's Guide on PHP and Database-driven Sites
PHP Tutorial Home | Step 1 | Step 2
STEP 2: OUR FIRST PHP SCRIPT – CAN YOU BELIEVE IT!
The excitement must be killing you … wow your first PHP script! First thing you need to do is create a fresh PHP page (just rename an HTML page from .html to .php*) and in between the <body> </body> tags, you will type in this PHP code … don’t worry I will explain soon enough:
<body>
<?PHP
print 'My first PHP script!';
?>
</body>
Remember I said that you needed to understand at least basic HTML to learn PHP? Well if you didn’t believe then, I ‘m sure you do now!
If you have no idea what <body> </body> mean, then you really need to do my web design tutorial or try my video tutorial ... it's not that hard!
HOW PHP IS LIKE HTML
Ok, for those of you who understand HTML, but are wondering about all that strange stuff in-between the <body> and </body> tags here we go!
PHP like HTML, has particularly formatted text that has special meaning. These 'tags' act almost like HTML tags (except in PHP, we call most of this text PHP code.)
So in the above example we see this first PHP tag:
<?PHP
This tells the PHP interpreter that this is the start of some PHP code.
Next we have this PHP code which acts as the closing tag ... (think HTML):
?>
This tag tells the PHP interpreter that this is the end of the PHP code. We can see here how PHP tags act like an HTML tags; there is an opening tag and closing tag.
AN EXAMPLE THAT COMPARES HTML vs. PHP
HTML:
<body> (html stuff in here) </body>
PHP:
<?PHP (PHP stuff in here) ?>
Back to our script:
<body>
<?PHP
print 'My first PHP script!';
?>
</body>
Between our main PHP tags (the PHP start and end tags) we have this PHP code:
print 'My first PHP script!';
This code will simply print the text: ‘My first PHP script!’ into your web page … not very exciting! Ok, let’s do something a touch more interesting - type in this PHP code:
print strlen("The brown cow makes brown milk!");
This code gives you something a little unexpected, it prints the number 31 on your page! Ok, what the heck is going on here! Well boys and girls, we just got a taste of the power of programming!
This PHP code is actually counting the number of characters between the quotes ("). The specific PHP code that is doing it is this:
strlen()
So the above PHP is saying: ‘print the number of characters in this sentence.’
The code strlen() is a built ‘thing’ in PHP that automatically counts the characters you tell it to count. You tell this thing what text/sentence to count by placing the text in-between the brackets after the PHP code: strlen
The great news is that PHP has hundreds of these ‘things’ ready to go – ‘things’ that can check if email addresses are good, send email and much more! BTW, these 'things' are called ‘functions’ – as if to say: they perform a function.
So we can say that: strlen() is a PHP function that counts the characters in a piece of text. Does that make sense? And if you think about it, the function 'strlen()' is short for: string length
You see, in the programming world, text is called 'strings'! You can think of it as 'a string of text'. So now you know when you hear a nerd say: "What is the length of the string?" You know they are talking about a piece of text.
A 'string' can be a single letter or number or any symbol on your keyboard. Or it could be a whole sentence or ten pages to text ... makes sense? OK, I’m getting ahead of myself here … let’s see this PHP code in action.
MAKING A PHP PAGE
Now that you have an HTML page with the PHP code in place, lets turn the HTML page into a PHP page. All you have to do is change your HTML pages file extension from .HTML to .PHP.
So for example, if your page was called:
MyPage.html
It now becomes:
MyPage.php
By giving the page the .php extension, you are telling the server that this page has PHP code in it and the server should then pass this page to the PHP interpreter to process it.
With the page extension switched to '.php', upload it to your server and then surf over to your page to see it in action. Remember, that the PHP code in your page can only work if it running from a server/computer that has the PHP interpreter installed - you cannot view the page by simple loading it your browser like you would a normal HTML page.
BACK - STEP 1
The next article will show you a couple of ways to install PHP on your machine - coming soon!
* Sometimes a PHP host will require that you name your .php pages (ex: index.php) to .php3 - but that is the old way of naming PHP pages and you should see this less and less.
TopCategories
- Ajax - Javascript
- Blogs and CMS
- Business of Web Design
- CSS
- Design
- Dreamweaver
- Drupal
- Flash
- General
- HTML
- Nerd Book Reviews
- Nerd Concepts in English
- Newsletter Archive
- PHP
- Podcast | Videos
- Professional Web Design
- Real World Web Design
- Ruby
- SEO & SEM
- Web Design
- Web Design News
- Wordpress
Other Websites
Fatal error: Uncaught Error: Undefined constant "Y" in /home/killersi/public_html/PHP/find-php-host-step2.php:171 Stack trace: #0 {main} thrown in /home/killersi/public_html/PHP/find-php-host-step2.php on line 171