PDA

View Full Version : anyone know how to code


crosshair
01-01-2010, 11:16 AM
ive been trying to learn how to code websites and stuff, but i can't quite understand PHP and a few other key components ..

if any one knows how to code well can help me it would be appriciated

Jefff
01-01-2010, 11:47 AM
What do you need help with?

crosshair
01-01-2010, 12:14 PM
well, ive been trying to make an rpg, that goes with my friends, basically like sibling sites, He said he used mostly html, css, and php. i know what html and css are for, thats pretty simple... but i dont understand the php part. and its confusing when i try to put it all together in the notepad and put it up

i know that is just the basics, and i need to get alot of other things, but ill try to figure that out

Jefff
01-01-2010, 03:04 PM
You don't need to know most of this, but I think it's helpful to know exactly what is happening;

When you are doing web development (usually) you use what is called CGI. CGI means common gateway interface, it is a standard that defines how a programming language interacts with a web server, mainly environment variables (https://secure.wikimedia.org/wikipedia/en/wiki/Environment_variable). For example, to find out exactly what url the user went to, you check the environment variable called `REQUEST_URI`. For output, anything that goes to standard output goes to the webbrowser (when you use `echo` in php, or have text outside of <?php and ?> that goes to standard output).

When a user goes to a script (assuming the server is set up right), that script is executed, and it's output is sent to the browser.

When you program in PHP, you aren't quite using CGI, there are a few ways the webserver can run your php scripts, and one of them is CGI (there is also fastcgi, which is similar to CGI, but faster, and some servers have a plugin for php, which is often even faster), but PHP hides that so that however the script is being run, it looks the same to your script (most languages don't do this). But that's not the point, the point is that PHP was designed like CGI, for example, to find out what url was asked for, you use the variable `$_SERVER['REQUEST_URI']`.

None of this is really important, you don't really need to know anything about CGI to make a website in PHP, but in my opinion, it really helps to understand what is going on when someone makes a request to your php script.

Basically;
- The PHP script generates the html*
- PHP gives you access to information about the user, you can find this in the $_SERVER, $_GET, $_POST and $_SESSION arrays (see docs on php.net for exactly what these arrays hold).

* You don't need to generate html, but that is the most common content to generate, you can also use php to generate any type of text file, and with some extra libraries, you can even generate images and flash, and many other types of files.

---
If you have any questions about this, feel free to ask.

[edit] As a basic example, try this out, it looks simple, but there are a lot of important pieces;
<?php
header('Content-Type: text/plain'); //This tells the browser that we are sending plain text,
//the default is html
if (isset($_GET['name']) {
echo "Hello, {$_GET['name']}";
} else {
echo 'Hello, I'm sorry, but I don't know what your name is';
}
?>

[edit 2] The importance of telling the browser that we are sending plain text, instead of html is so that the browser will no render it as html, for example, any linebreaks will be shown, and multiple spaces will not be collapsed.

Try going to the page, and then again with ?name=yourname at the end