Welcome Guest [Log In] [Register]
Welcome to New Nsider. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
  • Pages:
  • 1
  • 3
  • 4
PHP - Lesson 1; Introduction to PHP
Topic Started: Jul 15 2006, 02:57 PM (734 Views)
Cube
Member Avatar
White Pikmin
It is adviseable to know HTML before even attempting this lesson...web space can also be helpful.

Anyway, onto the basics...

The File:

PHP documents should end with a php file extension, otherwise the code will not be processed. For example...

File: test.php

...is valid. Now, let's move onto the php tags.

Start and End Tags:

For all php code, it must be within the following tags...

Code:
 

<?php
//Code here
?>


"//code here" is a comment, and therefore, not parsed by the php engine.

There are other tags it can be put into, but it's best to keep it within those.

PHP Comments:

What is a comment? Well, it's just that...a comment, usualy to make the code more understandable. Comments are not parsed by the engine, and effect nothing.

Single Line Comment...
//This is a line, commented. Change text as desired

Secondary Single Line Comment...
#This is as well, a commented line.

Block Comment...
/*
Look! This is all commented, this is a block,
which can be as long as you want it to be!
*/

The echo statement, and print() function:

The echo statement is used to output data, the print function does the same, but is, a function.

Echo statement...
Code:
 

<?php
echo "Hello world!";
?>


Print function...
Code:
 

<?php
print("Hello world!");
?>


Characters, are known as strings, and must always be enclosed in quotation marks (this is not the same for other datatypes), single (') or double ("). As well, statement and functions should usualy end with a semicolon (;), most of the time, you'll find this will result in a parse error should you not follow these rules.

HTML with...PHP?:

Now, let's work with combining HTML and PHP, there are a few ways to do this, but we'll go over the easiest.

Code:
 

<?php
$title = "This is a variable, we'll go over this soon";
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<?php
print('Google!'); //Look, it echoes google to the browser!
?>
</body>
</html>


As you can see, HTML is not contained in the PHP tags. But PHP is indeed included in this code.

Escaping Certain Characters:

Sometimes, you need to escape characters, for example...

Code:
 

<?php
echo ""Hello World!"";
?>


That will give you a parse error, so you need to escape them with a backslash (\)

Code:
 

<?php
echo "\"Hello World!\""
?>


Well, this concludes lesson 1, I'll post another today if I have time...

Lesson Assignments:

-Post the code you would use to echo "Hello World!" (with quotes, not using single quotes) in this thread.
-Then, add a comment to what the code does.
-Put it in the body of an HTML document.
-Post it.

Members who've completed this lesson:

Phantom123, B [Missing PHP Comment]
Speedy, A
Zero the Warrior, A

_Waffelz has turned off the smiley code for Cubert, so smileys don't go where they aren't supposed to_
Posted Image
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
And on another note...everyone is allowed to post. >_>
Posted Image
Offline Profile Quote Post Goto Top
 
Ganondorf10101
Member Avatar
Rebel Trooper
Pfft, PHP. I'm going to do some hardcore scripting in a few years.
*cries*
Posted Image
- Sig Redesigned by Ganondorf10101, Credit goes to arcHITect of NIC - Ganon10101
Offline Profile Quote Post Goto Top
 
SpeedyZ1
Member Avatar
White Pikmin
Edit: I'll edit this post soon.

Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Lesson assignments are up, at the bottom of the first post.
Posted Image
Offline Profile Quote Post Goto Top
 
Phantom123
Member Avatar
Covenant Elite
Code:
 
<?php
echo 'In soviet Russia, you no eat beef, BEEF EAT YOU!';
?>


What it does is, in the background, at the top of the page, it rights the text I mentioned.
---
This space will be used for the Other half of the assignment

Code:
 
<html>
<head>
<title>Monkey Feet</title>
</head>
<body>
<?php
echo '\'Bob\'';
/*Commentized-______-*/
?>
</body>
</html>


This combines PHP and HTML. It makes the basic Text-design of a Page.
<img src=http://pic.piczo.com/img/i15885113_21172_2.gif>

Best. Objection. Ever.
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Ok, Phantom, you've completed Lesson 1.
Posted Image
Offline Profile Quote Post Goto Top
 
Deleted User
Deleted User

._.

I don't get any of this. I think you need to explain what stuff means a little more...
Quote Post Goto Top
 
Phantom123
Member Avatar
Covenant Elite
Biggoron_2000
Jul 16 2006, 01:45 PM
._.

I don't get any of this. I think you need to explain what stuff means a little more...

He explains it plenty. He just puts too much>-<
<img src=http://pic.piczo.com/img/i15885113_21172_2.gif>

Best. Objection. Ever.
Offline Profile Quote Post Goto Top
 
Cube
Member Avatar
White Pikmin
Eh...here is a possibly more understandable version...

<?php
// Code goes here
?>

Now, <?php and ?> tell that it needs to be parsed (processed) as php. Therefore, any php code should go inside there. "// Code goes here" is a comment, the double slashes makes it that.

The echo statement, and print functions, aren't hard to use. They just require an argument to be useful. For example...

<?php
echo "this is where a string would go";
?>

A string, is what you usualy type in a post, letters, words, whatever. The double quotes (or single quotes) make it known that it's a string. That can be changed, but that's in lessons 2. The print() function, is nearly the same, but it's like this...

<?php
print("Look, another string");
?>

The semicolon, says that's the end of the function, process, or whatever.

Now, because the quotes/single quotes are what contains a string, it's wise to escape them if you want them displayed as text. So, if you want "Hello World!" to display in an echo statement, you'd do this...

<?php
echo "\"Hello World!\"";
?>

The backslash escapes 2 of the quotes, as you can see.


Posted Image
Offline Profile Quote Post Goto Top
 
Go to Next Page
« Previous Topic · Archive · Next Topic »
Add Reply
  • Pages:
  • 1
  • 3
  • 4

Theme created by tiptopolive. Find more great themes and skins at the ZB Theme Zone.