Jump to content

Recommended Posts

Posted

Here we're going to learn the basics of the PHP web scripting langauge in a very in depth tutorial. This includes variables, displaying text on a page, simple things like that. Note: This is not an experienced scripter tutorial. If you script PHP already, this tutorial is not for you. And if it was graphics, I'd make it an image, but it's not, so this post will only contain BBCode, and plain text. So here we go:

 

File format:

.php - The main PHP file extension.

.phps - A file format that displays the actual code without parsing it to the webserver (meaning if you have a news system, it would display the code you made, not the actual system).

 

Tools I use:

1. jEdit script editor - Found here

2.. The windows FTP system, or SmartFTP/leech FTP. It's honestly you're choice which one you prefer to use. And you can always check Google.

3. And last but not least, I use my secret directory on my website to see/test the results of my PHP scripts.

 

Table of content:

I - PHP Blocks

II- Display text

III - Comments

IV - Variables

V - Operators

VI - Else If

 

Now, let's start, shall we?

 

I - PHP Blocks:

There are two mostly used PHP blocks, and possibly a couple more if you look around the web. These are the two main ones:

 

<?php
code here
?>

and:

<?
code here
?>

 

If you plan on distributing any scripts you make, I advise you use the first block I showed you.. Some people just like it more. But if you're gonna keep your scripts private, it's entirely up to you; even though there's not really a difference.

 

II - Display text

As most of you reading this tutorial probably know HTML, CSS, XHTML, or something along those lines. When you use those, those are for design, making your website look good, etc. PHP is a scripting langauge, and is not entirely based upon looks and text, so you have to use the echo and print statement for displaying text. Echo is used more than print though, and people understand it off the back. Print is usualy recognized better in C++ code, and thing similar to that. Also remember, when creating a PHP script, use the HTML tags for defying the HTML, and for the body. The echo statement would look ike this:

 

<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>

 

On a webpage, that would display "Hello World". You may/should have noticed the semicolon at the end of the command line. When you end a instruction.

 

III - Comments

If you ever plan on distributing your scripts to place like.. Pixel2Life, HotScripts, or anywhere else where they have PHP scripts that users give in up for download, it's in your best interest to use comments. Comments are extremely helpful when people are reading your code to use it, or trying to learn from it, as well as also helps you keep notes. It explains what the code does without parsing any information from it in to the script. So it doesn't display at all, but is still viewable in the file.

 

They have two main comments:

 

<html>
<body>
<?php
/*
This is a multiline comment
*/

// This is a single line comment

#Shell style comment
?>
</body>
</html>

 

If you uploaded that on to your web server, the page would be completely blank. Those are the two main comments used in the PHP scripting langauge.

 

IV - Variables

In PHP, variables are used to store values, really. You can make a variable, display the variable's value on a web page, and a couple of other things. But right now we're going to focus on setting a variable's value, and then displaying it on a page. Remember: A variable always has a $ symbol infront of it.

 

Setting a variable:

 

<?php
$var = 'my first variable';
?>

 

Now $var would have the value of the text "this is my first variable". Also, two-word variables have the first letter lowercase, and the second word capital. Like this:

 

<?php
$myVar = 'my first two-world variable';
?>

 

Now the two word variable, $myVar, would have the value of the text "my first two-world variable".

 

Now we're going to display the variable using the echo statement:

 

<?php
$var = 'my first time displaying a variable'
echo $var;
?>

 

On your web page, that would display the text "my first time displaying a variable".

 

Say you'd like to display two variables, you'd do it with the dot operator. Like so:

 

<html>
<body>
<?php
$var = 'variable one'
$var2 = 'variable two'
echo $var . " " . $var2;
?>
</body>
</html>

 

That would display the value of $var, then $var2.

 

V - Operators

 

I'm just going to list all of the operaters used in the PHP scripting langauge (operater followed by action):

 

+ - Addition

- - Subtraction

* - Multiplication

/ - Division

% - Division remainder

++ - Inceremt

-- - Decrement

== - Is equal to

!= - Isn't equal to

> - Is greater than

< - Is less than

>= - Is greater than or equal to

<= - Is less than or equal to

&& - And

|| - Or

! - Not

 

VI - Else and If

Else and if are used in most aspects of scripting with PHP. They are used to test values (as well as other things), and then do an action when something is determined. In my example, I'm gonna use three options, and when you use three, or more.. It's if, elseif (depending on how many other options), then else is the final option.

 

Now, we're gonna try this:

 

<html>
<body
<?php
$ren='render';
if ($ren=="good")
echo "Good job!";
elseif ($ren=="okay")
echo "Not bad";
else
echo "No comment";
?>
</body>
</html>

 

Credits: Tyro

Posted

nice share man i was thinking to start learn about the programing but i didnt know from where to start i think that i will start from here :P

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...