PHP Writing Rule - Part 1

7/20/13

Welcome back!

Well i want to share some of PHP Writing Rule. It should be in the beginning of learning PHP, but i forget to tell you about this part.

Before that you need to learn how to display some code in browser, you can learn it from page : Excercise PHP-1

Okay,let's just start. PHP have some rule in their code. Here are some of the rule :

1. Two types of writing PHP
- Using <?php //statement ?>
- Using <? //statement ?>
The two above is same, but mostly people use the first one.

2. Using Semicolon (;) at the end of statement
<?php echo "We are rich!!!"; ?>
3. Use //comment or /* comment */  to type comment.
<?php $name = "Alex"; //this is variable name
/*echo "This is wrong, 
don't display it!";*/
?>
The different is // only for one line comment, but /* */ is for multiple line of comments.

4. String format in PHP

To display the string format we can use : echo,print, and printf/sprintf.
Echo : has 2 type of writing
<?php echo "Type 1";  //common use
echo ("Type 2"); ?>
Print : Nearly same as Echo, but have pros in escape character
<?php printf "This is PRINTF"; ?>
Printf/Sprintf : To display in string format with detail.
<?php $test = "There's only %d way %d say %d words %d you, %s";
 printf ($test,1,2,3,4,"I love You");
?>
The Code above will display : There's only 1 way 2 say 3 words 4 you, I love You

The table above will show you the type of data available for string format :

Symbol Table -  String that can be used in PHP
Symbol Table -  String that can be used in PHP

Okay there's more PHP writing rule, so see you at part 2!

No comments:

 

Tags