PHP and HTML – How to embed php into html

September 5th, 2009 § 0

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>&nbsp;</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.

 

php tutorial for newbie

September 4th, 2009 § 0

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.

 

How to become a php programmer?

August 26th, 2009 § 2

Do you want to become a php programmer? Learn below listed 7 essential languages or technologies before learning php language

PHP is the server side scripting language that can be used for producing dynamic web pages. PHP is generally used in the web development and it is embedded in to the HTML. Today PHP is the most popular language for web development. The php is a scalable, robust and a flexible language.

Do you want to become a php programmer (web developer)? There are certain languages or technologies to be learned before learning PHP. There is nothing like you can not learn php without knowing these things but if you know below listed technologies or languages, it will be very easy for you to learn and adopt PHP.

1. C is the best language to learn before learning any other programming language. It will help to understand how computer programs work. PHP would be much easier if you already know C programming, many concepts and syntax are same.

2. C++ is the object oriented programming language. Nowadays we have mainly OOPS based programming languages available. PHP also supports OOPS concepts; it will be good if you know all the concepts and working of OOPS.

3. SQL is the Structured Query Language for accessing and manipulating databases. Almost all applications used database to store data. And all available databases (MySQL, PostgreSQL, PostgreSQL etc.) follow almost same SQL standards.

4. HTML is the Hyper Text Markup Language. It is use to design web pages and display details. PHP is embedded into html to make the web pages dynamic. At first look html looks easy and simple, but having a in depth knowledge of html, is always beneficial to php programmer.

5. CSS (Cascading Style Sheet) is used in formatting the web pages.  Every website static or dynamic, they use CSS to manage look and feel of the website.

6. Javascript is a popular client side scripting language that is widely supported in Web browsers and other Web tools. It can interact with HTML source code. To design and develop Web2.0 standard web pages, JavaScript is mostly used.

7. PHP is the server side scripting language, you should aware of functioning of web servers, basic concepts like request, response etc.

Once you know above 7 languages/technologies, it will be easy for you to pick up PHP. PHP manual are best to learn php. And you can learn above listed things  from w3schools.com. And after having some experience as a php programmer, you can go for AJAX, Jquery, SMARTY and some php frameworks like zend, symphony, cakephp.

Good luck!

Where Am I?

You are currently browsing the php newbie tutorial category at echo "Himlal Pun";.