php vs c/c++ code structure comparison

What does PHP code look like?

What you will learn here.
1.How to escape from HTML and enter PHP mode 
2.Simple HTML Page with PHP 
3.Using conditional statements
4.Comments in PHP



Php code Structurally is similar to C/C++
Php scripting Supports procedural and object-oriented paradigm (to some degree) .
All PHP statements end with a semi-colon Each PHP script must be enclosed in the reserved PHP tag

learn php tutorials syntex



1.How to escape from HTML and enter PHP mode

•PHP parses a file by looking for one of the special tags that
tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag.
php with html embeded code
<?php echo “Hello World”; ?>

2.Simple HTML Page with PHP 

The following is a basic example to output text using PHP.



[code start]

<html><head>
<title>My First PHP script Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body></html> 

[code end]



Copy the code onto your web server and save it as “test.php”.

You should see “Hello World!” displayed. (if you dont know how to setup localhost/localServer then click here)

Notice that the semicolon is used at the end of each line of PHP code to signify a line break. Like HTML, PHP ignores whitespace
between lines of code. (An HTML equivalent is <BR>)

3.Using conditional statements

•Conditional statements are very useful for displaying specific content to the user. The following example shows how to display content according to the day of the week.  
[code start]
<?php
$today_dayofweek = date(“w”);

if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}

?>
 [code end]


The if statement checks the value of $today_dayofweek
(which is the numerical day of the week, 0=Sunday… 6=Saturday)
•If it is equal to 4 (the numeric representation of Thurs.) it will display
everything within the first { } bracket after the “if()”.
•If it is not equal to 4, it will display everything in the second { } bracket
after the “else”. 

If we run the script on a Thursday, we should see:
“Today is Thursday”.

On days other than Thursday, we will see:
“Today is not Thursday.”
[code start]

<?php
$today_dayofweek = date(“w”);

if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}

?>

 [code end]


4.Comments in PHP

•Standard C, C++, and shell comment symbols





[code start]

<?php
// C++ and Java-style comment
# Shell-style comments
/* C-style comments
These can span multiple lines */
?>
[code end]

Read next:

Variables in PHP



1 comment: