Basics of the PHP
Syntax
- A PHP script can be placed anywhere in the document.
- A PHP script starts with <?php and ends with ?>.
<?php
// PHP code goes here
?>
Conditional statements
The if Statement
The if statement executes some code if one condition is true.
The if...else Statement
The if....else statement executes some code if a condition is true and another code if that condition is false.
The if...elseif....else Statement
The if....elseif...else statement executes different codes for more than two conditions.
Loops
While
The while loop executes a block of code as long as the specified condition is true.
do...while
The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.
for Loop
The for loop is used when you know in advance how many times the script should run.
foreach Loop
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
String manipulation
The Length of a String
The PHP strlen() function returns the length of a string.
The Number of Words in a String
The PHP str_word_count() function counts the number of words in a string.
Reverse a String
The PHP strrev() function reverses a string.
For a Specific Text Within a String
The PHP strpos() function searches for a specific text within a string.
Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
No comments:
Post a Comment