PHP Getting Started
Setting Up a Local Web Server
PHP script execute on a web server running PHP. So before you start writing any PHP program you need the following program installed on your computer.
• The Apache Web server
• The PHP engine
• The MySQL database server
You can either install them individually or choose a pre-configured package for your operating system like Linux and Windows. Popular pre-configured package are XAMPP and WampServer. Coding our first Web Page:
First program in PHP:
<html> <head> <title>Hello World</title> <body> <?php echo "Hello, World!";?> </body> </html>
Save this text in the htdocs folder inside the web server installation and name the file helloWorld.php.
When we call now following in our browser the following address http://127.0.0.1/ helloWorld.php or localhost/ helloWorld.phpour script from PHP gets executed.
If the server is set up correctly and the PHP script could be executed, then the output is displayed within the browser.
Syntax Overview
A PHP script starts with the <?php and ends with the ?> tag.
The PHP delimiter <?php and ?> in the following example simply tells the PHP engine to treat the enclosed code block as PHP code, rather than simple HTML. <?php echo "Hello, world!"; ?>
Comment the code:
A comment is simply text that is ignored by the PHP engine. The purpose of comments is to make the code more readable. It may help other developer (or you in the future when you edit the source code) to understand what you were trying to do with the PHP.
Line comments start with two forward slashes //.
Multi-line comments begin with a slash and a star, /*, and end with a star and a slash, */
// This is a single line comment # This is also a single line comment echo "Hello, world!";
PHP is whitespace insensitive:
PHP whitespace insensitive means that it almost never matters how many white space characters you have in a row. one whitespace character is the same as many such characters.
PHP is case sensitive:
Variable $myText and $mytext are different.
Statements are expressions terminated by semicolons:
A statement in PHP is any expression that is followed by a semicolon (;). Any sequence ofvalid PHP statements that is enclosed by the PHP tags is a valid PHP program.
Expressions are combinations of tokens:
The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159),strings (.two.), variables ($two), constants (TRUE), and the special words that make upthe syntax of PHP itself like if, else, while, for and so forth.
Braces make blocks:
Although statements cannot be combined like expressions, you can always put a sequenceof statements anywhere a statement can go by enclosing them in a set of curly braces.
Variables (information stores):
Variables are used to store data, like string of text, numbers, etc. Variable values can change over the course of a script. Here're some important things to know about variables:
• In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
• After declaring a variable it can be reused throughout the code.
• The assignment operator (=) used to assign value to a variable.
In PHP variable can be declared as:
$var_name = value;
// Declaring variables $txt = "Hello World!"; $number = 10; // Displaying variables value echo $txt; // Output: Hello World! echo $number; // Output: 10
Naming Conventions for PHP Variables
These are the following rules for naming a PHP variable:
• All variables in PHP start with a $ sign, followed by the name of the variable.
• A variable name must start with a letter or the underscore character _.
• A variable name cannot start with a number.
• A variable name in PHP can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
• A variable name cannot contain spaces.
PHP Constants
A constant is a name or an identifier for a fixed value. Constant are like variables, except that once they are defined, they cannot be undefined or changed.
Constants are defined using PHP's define() function, which accepts two arguments: the name of the constant, and its value. Once defined the constant value can be accessed at any time just by referring to its name.
<?php // Defining constant define("SITE_URL", "https://www.tutorialrepublic.com/"); // Using constant echo 'Thank you for visiting - ' . SITE_URL; ?>
PHP Echo and Print Statements
The echo statement can output one or more strings. In general terms, the echo statement can display anything that can be displayed to the browser, such as string, numbers, variables values, the results of expressions etc.
<?php // Displaying string of text echo "Hello World!"; // Displaying HTML code echo "<h4>This is a simple heading.</h4>"; echo "<h4 style='color: red;'>This is heading with style.</h4>"; // Displaying variables $num = 10; echo $num; ?>
The PHP print Statement
You can also use the print statement (an alternative to echo) to display output to the browser. Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print().
<?php // Displaying string of text print "Hello World!"; // Displaying HTML code print "<h4>This is a simple heading.</h4>"; print "<h4 style='color: red;'>This is heading with style.</h4>"; // Displaying variables $num = 10; print $num; ?>