Toucan Flock
Would you like to react to this message? Create an account in a few clicks or log in to continue.

PHP Programming 101

Go down

PHP Programming 101 Empty PHP Programming 101

Post by Kaeliah Tue Mar 26, 2013 3:42 pm

This section is saddeningly empty so I'm going to share some of my knowledge about some basic programming principles that tend to trip people up. We'll start simple though. Smile All code is in PHP.


What is PHP?
PHP is a server-side programming or scripting language. This means that a .php file will be executed on the server or 'hard drive' it's stored on, rather than the user who is viewing file's browser. As a result you need to set up a server on your computer, or find a host that has the necessary software set up to run .php files. There are plenty of free hosts that offer this. Once you have a place to host these files you can start programming and running your .php files to create some really awesome applications. Facebook and Youtube are two examples of huge websites that are run on PHP scripts.


Starting Out
Most tutorials will have you start out writing phpinfo(); in a script to look at the particular settings that are turned on or off. I think this is kind of useless because there is no way I would expect a newbie to understand what's going on that page. So instead we'll do this:

Code:
<?php
echo 'Hello World';
?>

For anyone familiar with tag languages(as you should be already) <?php and ?> should look a tad bit familiar. These are the opening and closing tags for php scripts. You must place all your code between these two tags, but later down the line I'll show you some various ways of organizing PHP and HTML code on one page. The second thing we'll look at is echo. Echo basically means output, or print to the page. In this case we want the page to say 'Hello World' so that's what we tell it to echo. The two single quotes ( ' ' ) indicate that we are printing a string or a bunch of letters. The semi-colon at the end ( ; ) indicates the end of the line of code. Forgetting that semi-colon at the end WILL make your program explode (not work).

If you were to execute that code, your page should print out nothing but Hello World. You can experiment with adding HTML inside the single quotes, and seeing what get's outputted. But I'm going to move on to the next topic.

Variables
I also tell newbies to programming to imagine a variable as a little box that can hold one thing, and has a label on top. When you create a variable, you label it and store something some in it you wish to get later. Variables are extremely useful when the value you want to be there changes. For example if someone is logged into your website and you want to welcome them personally using their username, you need to put their username into a variable since you don't know WHO will be logged in. So the syntax.

Code:

$label = 'Hello World';

echo $label;

$ indicates a variable, and will be at the start of ALL variables. This is sometimes annoying, but also nice when reading the code. In this case we named the variable label. Then we used an equal sign ( = ) to assign or store our Hello World string. End the line, then we echo $label. Can you guess what will output? If you did it correctly, it should be the same as the last code. You can replace the label after the dollar sign with whatever name you want as long as the name STARTS with a letter or underscore and includes alpha-numeric characters (a-z, A-Z, 1-0, _). No Spaces, and you should remember that it IS case sensitive. So $label is not the same variable as $Label.

You can also assign numbers to variables, and then do operations on those variables. For example:

Code:

echo 4+5;

$x = 4;
$y = 5;

echo $x + $y;

The line echo 4+5; will output the result of that math operation: 9. Now if you assign the variable x the value of 4, and likewise you assign y the value of 5, and you add the two variables you get the same result. The code does not literally add x and y, but rather adds the value of what's in the variable box.
Variables are very complex, and so there will be more on this topic in more advanced tutorials.


Comments
Comments are an extremely useful tool to make your code clearer, and more obvious. It is generally very good practice to leave comments in the code for particularly vague or ambiguous(non-obvious) parts of the code. There are two types of comments that can be added to the code, and they are as follows.

Code:

echo 'Hello World'; // This is a comment.

// This is also a comment. echo 'Hello World';

/*
This is a multi-line
comment box.
*/

So the first comment we see, // This is a comment. looks a bit odd. The computer when reading the code COMPLETELY ignores comments. So as soon as it sees the double slash ( // ) it will go to the next line of code instantly. It doesn't even look at the comment. Comments are purely for the benefit of programmers. You can place a double slash or line comment anywhere on the line, but anything AFTER the double slash will be ignored. So in the second comment, the echo 'Hello World'; will not print out Hello World. It'll just be ignored.

The third comment in the code is a block comment. It is enclosed by a slash star combination ( /* */ ) and anything inside those tags will be ignored. You can have it be on just one line, or it can be a multi-line comment.

Commenting lines of a code out is a useful tool to find the error in a larger script.

Code:

echo 'Hello World';

echo 'Hello World";

If you tried to execute the code above, you'd get an error. Now some people can just look at this and identify the error, but for the purpose of explaining the process of commenting out pretend you don't see it. Razz So you don't know which of the two lines has the error in it, you can comment out the first line(but not the second line) and retry executing it. You will still get an error, so instead you uncomment the first line, and comment the second line. It'll work perfectly then. So you know that the second line has the error and you can examine it to find the error. Woops, that's a single quote and double quote in the same line, that won't work!

This wraps up PHP Programming 101 for now. Look out for more advanced topics in PHP if you are interested in going further. Also feel free to PM me for requests on tutorials for specific topics in PHP.
Kaeliah
Kaeliah
Community Advisor
Community Advisor

Posts : 43
Beak Points : 4328
Reputation : 5
Join date : 2013-03-26
Age : 31
Location : Millersville, Pennsylvania

http://www.createadopt.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum