PHP is used to create dynamic web pages, there are different ways to embed HTML code in your PHP pages. I believe you have read through my first session on php beginners.
- we can write html and php code separately using template system like Smarty, PHP Fast Template
- one more thing to remember, php code first executes on server and html is used to display the details on browser.
example1.php – embed html and php code together. dot (.) operator – returns the concatenation of its right and left arguments
<?php
$varMos = 3;
$userId = 5;
$pageName = "sisnolabs";
?>
<html>
<head>
<title>PHP and HTML </title>
</head>
<body>
<div align="center">
<?php
echo "This is PHP code";
?>
<br />
<?php
if ($varMos >= 3) {
echo "Good";
}
?>
</div>
<div> </div>
<?php
echo "<div>This is another PHP code </div>";
if ($userId > 0 && trim($pageName) != "") {
echo '<div><a href="index.php?id='.$userId.'&page='.$pageName.'">View Details</a></div>';
} else {
echo '<div><a href="index.php">Back</a></div>';
}
?>
</body>
</html>
example2.php – loop through array and display the details in html table. foreach – iterate over arrays.
<?php
$arrColors = array();
$arrColors[] = "Blue";
$arrColors[] = "Red";
$arrColors[] = "Green";
$arrColors[] = "Yellow";
$arrColors[] = "Pink";
$arrColors[] = "Purple";
$arrColors[] = "Orange";
?>
<html>
<head>
<title>PHP and HTML </title>
</head>
<body>
<table cellpadding="3" cellspacing="2" border="1" width="200" align="center">
<?php
foreach ($arrColors as $key=>$val) {
echo "<tr><td>".$key."</td><td>".$val."</td></tr>";
}
?>
</table>
</body>
</html>
will post next session soon, stay tuned.
I have produced some good php programmers and they are doing well in their respective companies, I am very happy for them, it gives me great satisfaction. Those are my close friends.
I believe, at sisnolabs I will make some better php programmers in coming years.
We have been working on open source technologies at sisnolabs and that inspire me to write php tutorial for beginners, this tutorial will have many posts. It will be a great achievement if I can tech them little php through my tutorial.
Who can follow this PHP tutorial
- if you know some html, C programming language and SQL
- if you are interested for web development
- if you support open source (even by using its products)
Get server ready
Have apache server ready (configured with php) in your machine to execute php scripts. Install xampp or wamp to your machine, it is the bundle of apache, php, mysql. (I believe you people can install it in your machine easily…Google or Bing it to download)
PHP editor
Have good php editor to write PHP code, here is a list of php editors
First PHP script – “Hello World”
- every php script has to start with <?php and end with ?> (however it is changeable)
- php statements should end with semicolon (;)
<?php
echo "Hello World";
?>
save this php script as index.php at root directory (document_root), it could be xampp/htdocs/ or wamp/www/ or var/www/public_html/
now use your browser (Firefox, Chrome, Internet Explorer, Safari, Opera) to access the script, URL can be like http://localhost/index.php or http://your_server_name/index.php
PHP variable
- do not need to declare data type for php variables
- PHP guesses the data type according to variable’s value
- every php variable should start with ($) sign
<?php
//numeric variable
$variableName = 100;
//string variable
$variableName = "hundred";
?>
Comments on PHP code
- single line commenting can be done using // or #
- multiple line commenting start with /* and end with */
<?php
//$strName = "sisnolabs";
#$arrColors = array();
/*
if (count($arrColors) < 1) {
echo "colors are missing";
}
*/
?>
I appreciate your feedbacks. Stay tuned for next session.