PHP With HTML!!
So, now what is PHP and why it is used?
In this article you will get answered to these questions. And will also get
basic knowledge about PHP.
PHP is a server side programming
language used to enhance your HTML built websites. As, this server side code can
be used to add a log-in screen, CAPTCHA code and many more to your website. It
redirects visitors to other pages or constructs a calendar.
Take a step ahead:-
Most of the times people don’t know how
to start and give up before getting start with it as sometimes it becomes bit
overwhelming to learn a new language. But, from my opinion learning PHP is not
as overwhelming as it seems to be. So, don’t be overwhelmed just take it one
step at a time and as soon as you knew it you will be off and running.
Know about HTML:-
As soon as you start with PHP you must
have some basic knowledge of HTML and if you already have then great take your
step towards PHP. In case if you do not have then there are plenty of HTML
articles and online courses or tutorials to help you out, just pick up these
resources and know about HTML. And, once you know both of these languages then
you can switch between these two within a same document. You can even run your
PHP codes from your HTML files.
Tools required:-
For creating your PHP pages you do not
need any different software as you can use the same software what you use for
creating your HTML pages. Simply to say any plain text editors can do this job.
You will also need an FTP client to transfer the files from your computer system
to your web host. Those who already have their websites most likely be knowing
about FTP program and using it too.
Grab some basics:-
Some basic skills which you guys need
to master first includes:-
ü How to start and end your PHP codes. You can start it using “<?php” and can
end it with “?>” .
ü How to write comments that are not going to execute in your code; there need is to just inform other programmers who are going to work on your code in
future or sometimes remind you of your thinking which you got while writing
this code. Below is the simple example:-
<?php
echo 'How to comment!'; // This example is a one-line c++/c style comment
/* This example is a multi line comment
yet another line of comment */
echo 'This is yet another comment';
echo 'One Final Comment'; # This example is a one-line shell-style comment
?>
echo 'How to comment!'; // This example is a one-line c++/c style comment
/* This example is a multi line comment
yet another line of comment */
echo 'This is yet another comment';
echo 'One Final Comment'; # This example is a one-line shell-style comment
?>
ü How to use the echo and print your statement as show in above example.
ü How to set your variables, see this example:-
<?php
$txt = "Hello world!";
$x = 10;
$y = 100.9;
?>
$txt = "Hello world!";
$x = 10;
$y = 100.9;
?>
ü How to use an array, see this example:-
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
ü How to use your operators and your operands? PHP divides the operators in
the following groups:-
· Arithmetic operators. Example:-
<?php
$x = 10;
$y = 6;
echo $x + $y;
?>
$x = 10;
$y = 6;
echo $x + $y;
?>
· Assignment operators. Example:-
<?php
$x = 20;
$x += 100;
echo $x;
?>
$x = 20;
$x += 100;
echo $x;
?>
· Comparison operators. Example:-
<?php
$x = 100;
$y = "100";
var_dump($x != $y); // returns false because values are equal
?>
$x = 100;
$y = "100";
var_dump($x != $y); // returns false because values are equal
?>
· Increment/Decrement operators. Example:-
<?php
$x = 10;
echo ++$x;
?>
$x = 10;
echo ++$x;
?>
· Logical operators. Example:-
<?php
$x = 100;
$y = 50;
if ($x == 100 xor $y == 80) {
echo "Hello world!";
}
?>
$x = 100;
$y = 50;
if ($x == 100 xor $y == 80) {
echo "Hello world!";
}
?>
· String operators. Example:-
<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>
· Array operators. Example:-
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x === $y);
?>
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x === $y);
?>
ü How to write conditional and nested statements. Example:-
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Learn to loop:-
Now, as you guys have mastered these
basic skills about the PHP, now this is the time to learn how to use loops in
PHP. A loop executes a code after evaluating a statement as a condition whether
it is true or false. If the passed statement is true then, it executes the
statement and change the original statement and starts over again and again by
re-evaluating the passed statement. And, this loop continues until the passed
statement becomes false. There are several different types of loops these are:-
Ø
While loop, example:-
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Ø
Do…while loop, example:-
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Ø
For loop, example:-
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Ø
Foreach loop, example:-
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
Learn different functions:-
To perform a specific task function is
used. You guys as a programmers can write a function in your code when you plan
to perform a specific task repeatedly. What you have to do is to write the
function once in you PHP code and use it where you want in your code, it saves
your time and space both. PHP also comes with a set of predefined functions
which you can use it easily in your codes. See below is a simple example on how to
define a function and how to use it:-
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
Conclusion:-
Now as you guys got the basic knowledge
of how PHP works, from here you guys just pick up few best tutorials, online
lectures or articles to grasp the PHP program to a higher level. From my
opinion W3Schools gives the best tutorial for learning PHP go through this link
to take PHP tutorial from W3Schools https://www.w3schools.com/php/ . I have also took the above examples from W3Schools so for better understanding go through this link.
No comments:
Post a Comment