Urgent Evoke

A crash course in changing the world.

PHP is a scripting language used to create dynamic web pages. Whenever you point your browser at a web address, the browser sends a request to the web server asking for the page. Often, the server simply has an HTML doc**ent stored on a disk somewhere, and it sends this doc**ent. However, if PHP is used, the server will execute the program code and send the result to your web browser. The code can do anything—query files on disk, create images, modify databases or bring in data from other websites.

If you've never programmed before, PHP is a good way to start. The reason is that it's easy to create programs that actually do something. PHP makes it easy to, for example, process input from a web form and do something with it.

Prerequisites: PHP code outputs HTML; the wh*** point of PHP is that it produces HTML to be read by a web browser. It can even be "woven into" HTML pages. You should at least know that there are opening and closing tags.

You also need a web server that will run PHP code. Many web hosts will provide this service. It's possible to run a web server on your own computer and try out PHP on it, but that's beyond the scope of this article. PHP itself is an add-on to a web server.

In this article I'm going to teach the basics of PHP. I'm going to go over the simplest knowledge necessary for using PHP in your webpages. Then I'm going to discuss strategies for continuing your PHP learning, along with some appropriate links.

PHP and HTML

A normal HTML page would look something like this.

<html>
<head>
<title>A normal HTML page</title>
</head>
<body>

<p>
Here's some HTML text.
</p>

</body>
</html>

This code resides in a file on the web server. When requested, the server will provide the file to browsers. However, if there is PHP code in the file (and the server is capable of running PHP), the PHP code will be executed.

PHP code is set off by a set of tags: <?php and ?>. Within these tags, PHP code exists. This code can be interspersed in HTML.

<html>
<head>
<title>A normal HTML page</title>
</head>
<body>

<?php
$text_0 = "This is some text. This text will be assigned to the text_0 variable.";
?>

<p>
Here's some HTML text.
</p>

<?php
$text_1 = "This is also PHP code. You can go back and forth between PHP and HTML.";
# Lines starting with a pound sign are comments.
$text_2 = $text_0;
# Note that $text_0 still exists in this fragment of code. It didn't vanish when we left the previous PHP segment.
?>

</body>
</html>

The two examples I've given so far will look exactly the same to the browser. That's because PHP never outputs anything unless specifically told to. (Of course, if there's an error, the error message will show up in the HTML.) To have the PHP code do something:

<p>
I'm not going to reproduce the entire HTML page every time.
<p>

<?php
echo "<p>\n";
$time_var = time();
echo "The time function returns: $time_var\n";
echo "</p>\n";
?>

The echo statement directs PHP to output something to the webpage. Now you can do calculations and display the results. There are several other important things to note about the fragment above.

A backslash followed by an n, "\n", is a newline character. It's like hitting Enter on your keyboard. If you don't have these, your text will all be put on one line. (In HTML that doesn't matter, but it's important to be aware of what's going on.)

Variables start with $. Variables are like locations where data is stored. Variables can be any type, from text to numbers to arrays of other data. (If you know about Perl, Perl arrays start with @ and not $, but in PHP everything starts with $.)

In double quotes, variables will be replaced by their values. The rules for when variables get expanded can be a little complex. ...

Functions have () after them. The time function returns the number of seconds since January 1, 1970. That might not seem useful, but there are functions to make use of that value, like the date() function. The point of functions is that they are names for sub-programs; the programs do some calculation and return a value.

Strategies

Start small. You can go from knowing nothing about PHP to creating an entire blog system...but it will take time. First, make a program that will output some text. Then make a program that will interpret form input. Then one that will access a database. Take small steps and make sure you understand each step along the way.

Experiment. This is the best way to learn computer programming. With PHP, as long as you're not using functions that access the disk, the worst things that can happen are 1) error messages or 2) infinite loops, which are annoying, but modern web servers will automatically stop them eventually.

Read the PHP manual. There's a wealth of information in a very clear style.

Have fun! The feeling of making a computer do what you tell it to is a rush unlike any other. ;)

Views: 40

Comment

You need to be a member of Urgent Evoke to add comments!

Join Urgent Evoke

Latest Activity

Ning Admin is now a member of Urgent Evoke
May 17, 2023
N updated their profile
Sep 25, 2020
Sophie C. commented on Asger Jon Vistisen's blog post Stinging Nettle
"I love that you've brought this to attention. An extensive database of uncommon but resistant and hardy plants/foods could be developed and organized by climate. Ease of growth and processing should also be taken in to account. I will try to…"
Aug 19, 2020
Meghan Mulvey posted a blog post

Fourth of July on the Lake

This past weekend was the annual celebration at the lake house in Connecticut. It is amazing that the lake is still so clear and beautiful after all these years. The watershed association has done a wonderful job protecting these waters from the damaging effects of development.The wood grill was finally ready to cook on, so we didn't miss the propane tank fueled grill anymore. The food actually tasted fresher than in the past and was easy to keep fueled.Dad was very proud of the solar hybrid…See More
Jul 6, 2020
Asger Jon Vistisen posted a blog post

Stinging Nettle

In this blog post I will focus on a plant that is abundant in our nature, and which is immensely nutritious. It's of course the Stinging Nettle. Let's start with the chemical constituents of this plant:37 % Non-Nitrogen-Extracts19 - 29 % Ash9 - 21 % Fiber4 % Fat22 % ProteinOnce the leaves are drid, their protein content can reach an astounding 40 %, which is much higher than beef, which even under the best of circ**stances can never exceed 31 % protein. In addition the Stinging Nettle consists…See More
Apr 13, 2020
Jonathon McCallum posted a blog post

The meal

It is 7'oclock, I was late home from work due to an assignment that i wanted to get ahead on. By the time I get home I am feeling extremley tired and I cannot be bothered to make a proper meal. I walk to the fridge and open it to see what there is for me to eat. All of the out of date foodstuffs have been automaticaly thrown away by the fridge, they will be recycled tomorrow as animal feed or something. I see i have organic local eggs and some local cheese. Foods are vacc** sealded for easy…See More
Mar 10, 2020
Jean Paul Galea shared a profile on Facebook
Mar 1, 2020
Kevin posted a blog post

Future

FutureToday is 2020/1/1. It is just like yesterday. The war is still continuing. It has started since 2010. In 2010, that year was a horrible year. Almost every energy ran out. Every country’s governments were crushed down at the same time. There were riots everywhere. All of the big company’s bosses were killed xdeadx in the riots. Troops fought each other everywhere. Food was bought up xawayx at once. There were no more food supplies in any shops. The economy was all crushed down. All the…See More
Jan 1, 2020
Namwaka Mooto posted blog posts
Jan 13, 2016
T D updated their profile
Sep 3, 2015
Brook Warner posted blog posts
Aug 25, 2015
Santiago Vega posted blog posts
May 5, 2015
Santiago Vega commented on Santiago Vega's blog post Act 8
May 5, 2015
Santiago Vega posted photos
May 5, 2015
Rico Angel Rodriguez posted blog posts
May 2, 2015
Rico Angel Rodriguez posted a photo

public servants

The exchange works directly for state and public workers and servants. It gives them credit in exchange for the amount of public work they contribute to the community. The more constructive they are based off a base rate the more credit they recieve.
May 2, 2015

Follow EVOKE on Twitter




Official EVOKE Facebook Page




EVOKE RSS Activity Feed










© 2024   Created by Alchemy.   Powered by

Badges  |  Report an Issue  |  Terms of Service