Because PHP runs on a server, you can’t easily check what it’s doing from the browser or developer tools. Therefore, when working with PHP, there will be lots of times when your code could execute perfectly fine, but you wouldn’t know – and, times when your code is completely wrong, but it doesn’t throw any errors.
To see what’s going on, and to directly insert the results of your PHP code into an HTML page, we have two methods: echo and print.
You’ve already seen the syntax for echo statements, which is simply
echo expression, …, expression;
As you can see, you can choose to echo only one thing, or a multitude of things (as long as they are separated by commas). These expressions can be anything – that’s what the rest of this course is all about.
If you’re only echo-ing one expression, you can use the alternative parenthesis syntax:
echo "<p>This is a paragraph that has been dynamically inserted by PHP. How great!</p>";
The print statement does the same as echo statements, but with these differences:
- It only accepts a single expression.
- It’s marginally slower than echo.
- It returns the value 1.
Again, it has two equivalent syntaxes:
print(expression); print expression;
The fact that this function returns value one, simply means that the result of using it is an expression itself (unlike echo statements). This means that you can chain multiple
echo keyword can only be at the start of a line.
print 5*5*5; //Displays 5*5*5 = 125 on the page print(5*5*5); //Does exactly the same.
Why Two Syntaxes?
Because both of these methods are fundamental and crucial to the PHP language. Without them, you wouldn’t be able to do anything (and that’s no exaggeration). Almost all other functions in PHP, no matter how many parameters they accept, require the parentheses.
To differentiate between those different kinds of functions, we call fundamental functions with those two syntaxes language constructs. Language constructs are more than functions; they are the building blocks of the language.
Functions? Parameters? What?
As you can see, functions play a large role in any programming language. Functions are nothing more than blocks (consisting of lots of statements) with a name, so that you can call it from anywhere within the program.
Nearly every statement you will write, will be a call to some function. Lots of these functions are built-in, and allow you to do crucial things such as get the length of a piece of text, connect to a database, or perform complex math operations. (You can also create functions yourself.)
But, those functions usually require some values to work – values which you must provide. For example, to get the length of a piece of text, we need to know which piece of text we want the function to work on. The values a function accepts are called parameters. The actual values you give it in a certain instance are called arguments. When a function gives you back a value, such as the length of a piece of text, we say that it returns that value.
echo strlen("PANDA"); /* The word above, strlen(), is a function that determines the length of a piece of text * It accepts one parameter, which is the piece of text we want to examine * In this case, we give it the argument "PANDA" * Then, it returns the length of the word "PANDA", which is 5. * This value, 5, is displayed in the browser by echo */
Don’t worry, functions and the like will be discussed in great detail later in the course, I just wanted to get some terminology out of the way.