The initial step in building or developing dynamic websites with PHP and MYSQL is studying the fundamental basis of PHP itself. In this tutorial you will learn the basic - basic PHP, ranging from syntak PHP, variables, operators, constants and other basic PHP.
Basic Syntax
As previously described, to add PHP code into a page, you must menaruhya in PHP tag
<? PHP
?>
Everything that is placed inside the PHP tag will be executed by the
web server as PHP code, and text outside the PHP tags will be
immediately sent to the web browser as plain HTML.
Sending Data to the Web Browser
To create a dynamic website, you must know how to send data to a web browser. PHP provides many functions to do this, the most widely used are print () and echo (). To send a text "My name is PHP" to the web browser is very easy:
echo "My name is PHP"; or echo 'My name is PHP';
or you can use print ():
print "My name is PHP"; or print 'My name is PHP';
as you can see, the use of echo and print to send text / data to the browser is to put the text / data to be sent in two quotation marks "or single quotes' and ends with a point of Kome;.
So what if the text is sent with a flag picking two or quotes one
example, "Andi said," How are you? '"or" Andy's dog ";. There are two
ways that you can use, the first if the text is sent containing the
quotation marks two may be placed in quotation marks one such as:
echo "Andi said," How are you? " ';
if the text is sent contains a single quotation mark you can put them in quotation marks two such as:
echo "Andi's dog";
The second way is to use the backslash sign (escaped string) as follows:
echo "Andi said," How are you? '";
echo 'Andi's dog';
echo 'Andi's dog';
Write a Comment
Like any other programming language, PHP can also be inserted with a comment to remind you about something. To write a comment in PHP there are three ways, the first is to use the sign #
# This is a comment
The second is to use 2 pieces of slash
/ / This comment
echo 'helo word'; / / say helo word
echo 'helo word'; / / say helo word
The third is enclosed in / * * /
/ * This is a comment
shape when * /
shape when * /
The third way can be used to write a comment more than one line. while one and two-way used to write a comment in one line. Comments will not be executed by the web server.
PHP Variables
A variable is a place to store data temporarily. The value of a variable can be text, numbers or other data. PHP has 8 types of variables: boolean (true - false), integer, floating point (decimal), strings, arrays, object, resource, and null. The rules of writing in php variables are as follows:
- A variable must begin with a dollar sign $ instance, $ namaVariabel;
- The variable name can contain any combination of letters, numbers and underscore for example, $ customer_1;
- The first character after the dollar sign must be a letter or underscore, should not be numbers.
- variable names in PHP are case-sensitive, meaning that $ namaVariabel differ with $ Namevariabel.
Variable is filled values (in-asign) with an equal sign = for example, $ name = 'Andi'. And variables can be displayed without the quotes such as:
echo $ name;
print $ name;
print $ name;
or it can be shown with two quotation marks:
echo "Hello, $ name";
you can not display variables with pertik mark one:
echo 'Helo, $ name'; / / this will not work, either.
to more clearly in the use of the variable will be given an example. Follow the steps below.
- Create a new document with your text editor.
- Create an opening php tag and the contents of a comment
<? PHP # example of using variables - create a variable with the following script:
$ File = $ _SERVER ['SCRIPT_FILENAME'];
$ _SERVER Is actually a variable that contains information about the server. the variable $ _SERVER ['SCRIPT_FILENAME'] contains the name of the script being executed, for example, C: xampphtdocsvariable.php. The script above is to create a new variable named $ file and populate a new variable $ file with the value of the variable $ _SERVER ['SCRIPT_FILENAME']. - The second variable created with the script below:
$ User = $ _SERVER ['HTTP_USER_AGENT'];
$ _SERVER ['HTTP_USER_AGENT'] contains the value of a web browser and operating system used to access the script. The script above is to create a new variable with name $ user and fill in the variable $ user to the value of $ _SERVER ['HTTP_USER_AGENT']. - type the script below to display the script that is being accessed:
echo "<p> you run the script: <br> <b> $ file </ b> </ p>"; - create a script to display the information used Bowser:
echo "You use the <p>: <br> <b> $ user </ b> </ p>"; - Complete PHP and HTML code:
?>
</ Body>
</ Html>
The end result is as follows:<? PHP # example of using a variable
/ / First variable
$ File = $ _SERVER ['SCRIPT_FILENAME'];
/ / Second variable
$ User = $ _SERVER ['HTTP_USER_AGENT'];
/ / First variable manmpilkan
echo "<p> you run the script: <br> <b> $ file </ b> </ p>";
/ / Display the second variable
echo "You use the <p>: <br> <b> $ user </ b> </ p>";
?>
- Save with variabel.php name, place it in the directory of your web server and test using your browser.
