01 · THE IDEAREAD → TRY → LEARN
What a PHP program looks like
A PHP file is ordinary text that the PHP engine reads. The engine only treats text as code when it sits inside PHP tags. The standard opening tag is <?php. Everything after it is read as PHP instructions until the engine reaches a closing tag ?> or the end of the file. In a file that contains only PHP, it is common to leave the closing tag out.
The tags matter because PHP can be mixed with other text, such as HTML. Text outside the tags is sent to the output unchanged, and text inside the tags is run as code. In this course the examples are pure PHP, so every one begins with <?php.
Your first output
The simplest useful program prints one line. The language construct echo sends text to the output. A string in quotes is a value, and the semicolon ends the statement.
echo "Hello, PHP!\n";Hello, PHP!
The \n inside double quotes becomes a newline character. Without it the output would end without a line break. Run the example and compare the result with the expected output shown beside it.
Statements, variables and comments
PHP code is a sequence of statements. Each statement normally ends with a semicolon, and that semicolon is how PHP knows where one instruction stops and the next begins. Whitespace and line breaks between statements are for people; the engine does not need them.
A variable starts with a dollar sign, such as $language. You create it by assigning a value with =. Comments are notes that PHP skips. A line starting with // is a comment, and # works the same way. A block comment is written between /* and */.
// A single-line comment
$language = 'PHP';
$version = 8.4;
echo $language, ' ', $version, "\n";
echo 'Done', "\n";PHP 8.4 Done
Notice that echo accepts several values separated by commas, and it prints them in order. The number 8.4 is a float, and PHP prints it as 8.4.
Printing versus returning
Beginners often mix up two ideas: printing a value and returning a value. Printing sends text to the output, where a person can read it. Returning hands a value back to the code that called the function, so that code can store it, test it or print it later.
A function is declared with the function keyword, a name, parameters in parentheses and a body in braces. A type after the colon, such as : string, states what kind of value the function returns. The return statement ends the function and passes that value back.
function welcome(string $name): string
{
return 'Welcome, ' . $name . '!';
}
$message = welcome('Ada');
echo $message, "\n";
echo strlen($message), "\n";Welcome, Ada! 13
The dot . joins strings together. The function never prints anything itself. The code outside it stores the result in $message, prints it and then asks strlen() how many characters it contains. Because the function returned a value, we could reuse it twice.
Worked reasoning
Before you write a function, say what goes in and what comes out. For welcome, the input is a name and the output is a sentence. Then build the output from the input step by step: take the name, join it with the fixed text and return the result. Keeping this order makes functions easy to test, because a test can call the function and compare the returned value without caring about screen output.
Exercise contract
Write a function named greeting that accepts one string and returns a string.
- Remove spaces from both ends of the name with
trim(). - If nothing is left after trimming, use
Worldas the name. - Return
Hello,followed by the name and an exclamation mark. - Return the string; do not print it.
For example, Ada becomes Hello, Ada!, and an empty string becomes Hello, World!. The checks call your function and inspect the returned value, so a solution that only uses echo will fail.
Recap
- PHP code lives inside
<?phptags, and pure PHP files may omit the closing tag. - Statements end with semicolons, and comments are skipped by the engine.
echoprints values, whilereturngives a value back to the caller.- Describe the input and output of a function before you write it.
No prerequisites. Start here.
- Recognize PHP tags and statements
- Print a value and return a string from the supplied function