much like a document, php reads things from the beginning of the file to the end. what happens on the first line comes before what happens on the second line, for example. this is important for many reasons. but we can start with one of the simplest.
you have already seen how to write things in php. there are several ways to do it. one is the print_r command you have already seen. another is echo. every example here uses the print_r command and the reasons for that are complicated but you can assume, if you see echo anywhere in other examples, it does much the same thing. there are limitations to the use of echo and its partner print that don't apply to print_r and i believe it is easier to standardize on using one instead of having to go back and change later.
if we look at an example...
print_r("hello");
print_r("there");
print_r("everyone");
you will see three commands with a predictable outcome -- to an extent. we can learn a few basic things about writing php from this before we even look at what the output from that code will be.
the first is that every command in php, regardless of what it is, must end with ;. you can think of a semicolon as the equivalent of "i'm done" or "ok, go ahead". if it doesn't end in ;, php doesn't think it's complete and will assume you're just continuing. even if it's on another line. remember, whitespace doesn't matter to php and that includes new lines.
the second is that many commands take a series of parameters and those parameters are contained between ( and ). we will talk about the different types of commands soon but a good rule to remember is that parameters are wrapped in brackets.
a parameter is just a piece of information passed to a command. in this case, text like "hello", "there" and "everyone" is passed to the command print_r by wrapping it in brackets. then each command is finished with ;.
if you look at any php program, you will see that this pattern repeats over and over. you should now be able to look and identify what is a parameter passed into a command and where each command ends.
what might surprise you, however, is what the output of this short program looks like.
hellothereeveryone
why no spaces or new lines? simple. because the only thing we told it to write was those letters. php did exactly what we instructed but no more or less. how would we make sure there were spaces between the words? we could just add a space inside the "" in our original program.
print_r("hello ");
print_r("there ");
print_r("everyone");
would now return
hello there everyone
the thing to remember from this is that the result looks like what you said you wanted, not necessarily what you wanted or what you thought would be a human-friendly version of what you wanted.
there are two things you need to know before you can make a program run in php. the first is that php files need to be known to be php by the server. in the case of apache, nginx and most others, this is as simple as calling them .php. the default file in any web server's directory is called index.php but you can use any name you like. remember, your server will probably be case-sensitive so there is a difference between hello.php and Hello.Php.
if your web server is running at mydomain.com and you call your file index.php, for example, you can just open mydomain.com in your browser and it will run. if you call it something else, you will need to open mydomain.com/myfilename.php to tell it where the file is.
the other thing you need to do is tell the server to execute php code inside the file. there are reasons for this that i consider outdated but the organizations responsible for php have decided to continue to require including it. you have to start your file with <?php (preferably on a line by itself) so the server knows to run it through php.
so the program we just looked at will look like this...
<?php
print_r("hello ");
print_r("there ");
print_r("everyone");
from here on, i will assume every php file you write already begins with <?php and it won't show up in the examples. don't forget it, though, or your web server will just show the contents of the file instead of running it. there are ways around this but it's easier just to include those five characters at the beginning of each file than messing around with the configuration.
(there are some complex reasons why this is used but they're not relevant or useful to understand at this point and there are many thousands of debates on the internet about whether they should be retained. suffice it to say it's a technical issue that doesn't impact anything you'll learn here.)
at this point, you should be able to write that code in a file, save it on your web server and open it in your web browser, providing predictable results. if that isn't working for you, stop here and make sure you have installed your web server correctly, have named and saved your file in the right place and are opening the right address in your web browser.
assuming you see hello there everyone in your browser, we can move on and start with more of the basics of php. you can continue to use the same testing file for what you write or create a new one for each example. as long as they're named .php at the end, it shouldn't matter.
anything that isn't html, of course, might look a little strange in the browser window. for examples that return something that's just plain text, you should view the source instead of what shows up in the browser window to see accurate results.
you can store information in many ways but the two main ones are constants and variables. the difference is in the names. a constant is a piece of information you set once and can never change. a variable is a piece of information you can change.
in php, constants are usually just named using text. there are some conventions but no real rules. variables must start with $. if you see something starting with a dollar-sign, you can assume it's a variable. if you see something where you think a variable should be but it doesn't begin with $, you can assume it's a constant.
php on a web server already includes some constants and variables that can be accessed within your program. we will talk about those later but they are named just like ones you'll create yourself. and yes, if they are variables, even if they are set by the web server, you can change them -- because they are variable, like the name says.
variables can contain many different types of data, not just text. they can be numbers, dates, collections of things, even whole files or documents. they can also be empty. if a variable is empty, its value is null.
the simplest way to set a variable is using the = character.
$myFirstVariable = "hello, everyone. i am a variable.";
we now have a variable named $myFirstVariable and it contains the text hello, everyone. i am a variable.. there are several important things to notice here. one is that text needs to be wrapped in "" (or something else in some special cases but definitely wrapped in something). those characters don't appear in the stored information. they're just a way to tell the program it's text.
another is that everything is case-sensitive. $myFirstVariable is different from $myfirstvariable or $MYFIRSTVARIABLE. all three are completely valid names but they are three different variables so it is important to be consistent.
we will follow a simple rule when it comes to naming things and capitalization here. this is not universally-applied in the programming world but it is one of several options that are common. the first word is lowercase and every word after will begin with a capital letter. special characters like accents will be simplified to their non-accented equivalents and anything other than letters and numbers will be spelled out.
you certainly don't have to name things in english if you speak another language. just remember that computers sometimes treat special characters differently and can get confused so i recommend using nothing other than a-z, A-Z and 0-9 to name things. inside "", you can use any characters you like. (there are some potential problems with special characters but that isn't related to php -- it is something you may have to solve in your web server and most modern software has standardized on utf-8 as a solution.)
you probably won't set many constants at first but they are set like this...
const myFirstConstant = "hello, everyone. i am a constant.";
if we take the things we've just learned and combine them...
$myFirstVariable = "hello, everyone. i am a variable.";
print_r($myFirstVariable);
it will return...
hello, everyone. i am a variable.
one more thing to make your life easier as you learn. to add a line-break, simply put \n in a line of text. as a result, this...
$myFirstVariable = "hello, everyone.\ni am a variable.";
print_r($myFirstVariable);
will return...
hello, everyone.
i am a variable.
line-breaks are rarely important when returning html but it is very useful for learning when we are just looking at plain text.
one of the simplest things to do in php is math.
print_r(5*5);
returns...
25
so we can combine that with what we know about variables...
$firstNumber = 85;
$secondNumber = 5;
$thirdNumber = $firstNumber / $secondNumber;
print_r($thirdNumber);
returns 17.
note that you don't need to use "" when you're working with numbers. that only applies to text.
"strings" are what we call blocks of text or any other content we want to treat as if it's text. when we want to combine two numbers, we can add them using +. some languages also combine text using the same symbol but php differentiates between combining things and adding them. for example, if you take 5 and 1, you can add them and get 6 or combine them and get 51.
php uses + to add and . to combine. the technical term for this combination is "concatenation", often shortened to "cat". but you can think of it as joining things one after another.
$first = "a";
$second = "p";
$third = "p";
$fourth = "l";
$fifth = "e";
$result = $first . $second . $third . $fourth . $fifth;
print_r($result);
returns apple.
there are lots of special characters you can use inside strings but the one you will probably encounter frequently is ". we already know strings are wrapped in "" when we set them so how do we include quotation marks inside our strings? simply add \ before each and it will treat it as a character instead of the beginning or end of the string.
print_r("she said \"don't worry\" but i was still afraid.");
returns she said "don't worry" but i was still afraid..
your code should make sense to someone reading it. if it's not immediately apparent what something does, it's a good idea to try to write it in a way that makes it more obvious. if it's still unclear, though, you can add documentation inside your php file to remind you or help the next person to read your code. we call this "commenting".
add // at the beginning of any text and it will be ignored by the computer but stay in the file for humans to read. you can do this on a line by itself or at the end of a line.
// this sets a variable to 1 hour in seconds
$hourInSeconds = 3600;
or...
$dayInSeconds = 86400; // 1 day in seconds
you will sometimes see long comment blocks between /* and */ but this is often confusing. it is much easier to keep comments simple and use // before every line to make it clear.