06 June 2009

PHP Basics At A Glance

If you have ever learnt PHP, then this blog can help you to remember the basic and syntaxes of PHP.

1. PHP Tag for scripting:

<?php
?>

2. Printing : two commands are available

echo "Hello World!!";

print "Hello World!!";

3. Simplest php file:

<html>

<body>

<?php

echo "Hello World!!";

?>

</body>

</html>

4. Be careful about semicolon (;) after each command.

5. 2 styles of comments

// this is a line comment
/* This is a
multiline comment
*/

6. Variable names starts with ‘$’ sign. Note: PHP is a loosely typed language.

$myStr = "This is my string";

7. String concatenation operator is dot (.), not plus (+)

$myText = "Your name is " . $yourName;

8. If… elseif… else statement:

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

9. switch statement:

switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}

10. Array:

$persons = array("Asim","Snigdha","Ashis");
$personAges = array("Asim"=>34, "Snigdha"=>30, "Ashis"=>26);

11. while loop:

while (condition)
{
code to be executed;
}

12. do-while loop:

do
{
code to be executed;
}
while (condition);

13. for loop:

for (init; condition; increment)
{
code to be executed;
}

14. foreach loop:

foreach ($array as $value)
{
code to be executed;
}

15. function:

function functionName()
{
code to be executed;
}

16. Form, $_POST

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

welcome.php:

<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

17. $_GET is used for form submission method = "get".

18. $_REQUEST contains the content of all of $_GET, $_POST, $_COOKIE

0 Comments:

 

© 2007 t!ps n tr!cks: PHP Basics At A Glance



Template unik dari rohman


---[[ Skip to top ]]---