My first PHP script!
My first PHP page
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.
However; all variable names are case-sensitive!
My car is red
My house is
My boat is
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.
Using comments to leave out parts of the code:10In PHP, a variable starts with the $ sign, followed by the name of the variable:
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have experienced coming back to their own work
a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
PHP supports several ways of commenting:
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).Hello world!
Rules for PHP variables:
A variable starts with the $ 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 can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ( and are two different variables)
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
I love W3Schools.com!
The following example will produce the same output as the example above:I love W3Schools.com!
The following example will output the sum of two variables:9
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
Variable with global scope:
Variable x inside function is:
Variable x outside function is: 5A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
Variable with local scope:
Variable b inside function is: 5
Variable b outside function is: PHP The global Keyword
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):
PHP also stores all global variables in an array called .
The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
The example above can be rewritten like this:
PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted.
However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:
Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.
PHP echo and print Statements:
With PHP, there are two basic ways to get output: echo and print.
In this tutorial we use echo or print in almost every example.
So, this chapter contains a little more info about those two output statements.
PHP echo and print Statements
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions.
echo can take multiple parameters (although such usage is rare) while print can take one argument.
echo is marginally faster than print.
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo().
The following example shows how to output text with the echo command (notice that the text can contain HTML markup):
PHP is Fun!Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.
The following example shows how to output text and variables with the echo statement:
Learn PHPStudy PHP at W3Schools.com