IWS2022 - Tutorial 1 - Install Xampp and Introduction To PHP (Part 1)
IWS2022 - Tutorial 1 - Install Xampp and Introduction To PHP (Part 1)
I. Install applications
1. Install Xampp
You can visit the below link to download and then install Xampp on your
computer. In this course, we will use the version 7.4.27.
https://www.apachefriends.org/download.html
2. Install Sublime Text 3 or 4 (Optional)
If you are not having a text editor or IDE for PHP coding (such as PHP Storm,
Visual Studio Code…), Sublime Text can be a good choice for you.
https://www.sublimetext.com/download
Note: If you use Sublime Text, there are some useful support packages that you
can install to support your work.
http://code5s.com/sublime-text-3-va-cac-plugin-can-thiet-cho-lap-trinh-laravel/
II. Basic of PHP
1. Write the “Hello World”
If you installed Xampp in the default location, please go to the directory C:\
xampp\htdocs then create a folder Tutorial01 and a new PHP file named
tutorial01.php inside it.
Open it with the text editor and write the following lines.
Start the Apache server on Xampp, then use your browser to go to link
localhost/Tutorial01/tutorial01.php
You can see the “Hello World” on the screen.
2. Syntax overview
a) Escaping to PHP
<?php...?>
If you use this style, you can be positive that your tags will always be correctly
interpreted.
Single-line comments
Multi-lines comments
Indenting and Line Length − Use an indent of 4 spaces and don't use any tab
because different computers use different setting for tab. It is recommended to
keep lines at approximately 75-85 characters long for better code readability.
Control Structures − These include if, for, while, switch, etc. Control statements
should have one space between the control keyword and opening parenthesis, to
distinguish them from function calls. You are strongly encouraged to always use
curly braces even in situations where they are technically optional.
Function Calls − Functions should be called with no spaces between the function
name, the opening parenthesis, and the first parameter; spaces between commas
and each parameter, and no space between the last parameter, the closing
parenthesis, and the semicolon.
Comments − C style comments (/* */) and standard C++ comments (//) are both
fine. Use of Perl/shell style comments (#) is discouraged.
PHP Code Tags − Always use <?php ?> to delimit PHP code, not the <? ?>
shorthand. This is required for PHP compliance and is also the most portable way
to include PHP code on differing operating systems and setups.
Variable Names −
Make Functions Reentrant − Functions should not keep static variables that
prevent a function from being reentrant.
One Statement Per Line − There should be only one statement per line unless the
statements are very closely related.
Short Methods or Functions − Methods should limit themselves to a single page
of code.
4. Variable Types
Here are the most important things to know about variables in PHP.
All variables in PHP are denoted with a leading dollar sign ($).
The value of a variable is the value of its most recent assignment.
Variables are assigned with the = operator, with the variable on the left-hand
side and the expression to be evaluated on the right.
Variables can, but do not need, to be declared before assignment.
Variables used before they are assigned have default values.
PHP does a good job of automatically converting types from one to another
when necessary.
PHP has a total of eight data types which we use to construct our variables −
A variable that has been assigned NULL has the following properties −
Strings that are delimited by double quotes (as in "this") are preprocessed in both
the following two ways by PHP −
Certain character sequences beginning with backslash (\) are replaced with
special characters
Variable names (starting with $) are replaced with string representations of
their values.
The escape-sequence replacements are −
Scope can be defined as the range of availability a variable has to the program in
which it is declared. PHP variables can be one of four scope types −
Local variables
Function parameters
Global variables
Static variables
5. Constants
The full path and filename of the file. If used inside an include,the name of the
included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path
whereas in older versions it contained relative path under some circumstances.
3 __FUNCTION__
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the
function name as it was declared (case-sensitive). In PHP 4 its value is always
lowercased.
4 __CLASS__
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name
as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
5 __METHOD__
The class method name. (Added in PHP 5.0.0) The method name is returned as it was
declared (case-sensitive).
6. Operator Types
a) Arithmetic Operators
b) Logical Operators
c) Assignment Operators
if...else statement − use this statement if you want to execute a set of code when a
condition is true and another if the condition is not true
elseif statement − is used with the if...else statement to execute a set of code if one
of the several condition is true
switch statement − is used if you want to select one of many blocks of code to be
executed, use the Switch statement. The switch statement is used to avoid long
blocks of if..elseif..else code.
8. Loop
do...while − loops through a block of code once, and then repeats the loop as long
as a special condition is true.
The PHP break keyword is used to terminate the execution of a loop prematurely.
The break statement is situated inside the statement block. It gives you full control
and whenever you want to exit from the loop you can come out. After coming out
of a loop immediate statement to the loop will be executed.
The PHP continue keyword is used to halt the current iteration of a loop but it
does not terminate the loop.
Just like the break statement the continue statement is situated inside the statement
block containing the code that the loop executes, preceded by a conditional test.
For the pass encountering continue statement, rest of the loop code is skipped and
next pass starts.
9. Arrays
Numeric array − An array with a numeric index. Values are stored and accessed
in linear fashion.
Associative array − An array with strings as index. This stores element values in
association with key values rather than in a strict linear index order.
Multidimensional array − An array containing one or more arrays and values are
accessed using multiple indices
A multi-dimensional array each element in the main array can also be an array.
And each element in the sub-array can be an array, and so on. Values in the multi-
dimensional array are accessed using multiple index.
10. Strings
Singly quoted strings are treated almost literally, whereas doubly quoted strings
replace variables with their values as well as specially interpreting certain character
sequences.
There are no artificial limits on string length - within the bounds of available
memory, you ought to be able to make arbitrarily long strings.
Strings that are delimited by double quotes (as in "this") are preprocessed in both
the following two ways by PHP −
Certain character sequences beginning with backslash (\) are replaced with
special characters
Variable names (starting with $) are replaced with string representations of
their values.
To concatenate two string variables together, use the dot (.) operator
b) Using the strlen() function
The strpos() function is used to search for a string or character within a string.
If a match is found in the string, this function will return the position of the first
match. If no match is found, it will return FALSE.