Introduction - ranjanui/php GitHub Wiki
<?php [code here] ?>
<script language="php"> [code here] </script>
<? [code here] ?>
<?= [output here] ?>
<?php echo "some output"; ?>
<?php
$myVar = "Hello world!";
echo "<b>" . $myVar . "</b>";
?>
<?php
$myVar = "Hello world!";
echo $myVar;
?>
<?php
$number = 42;
if($number == 42) //double equal
echo "The number is 42!";
?>
<?php
$number = 42;
if($number === "42") //triple equal
echo "The number is 42!";
?>
<?php
$animal = "Cat";
if($animal == "Dog")
echo "It's a dog!";
else
echo "I'm sure it's some sort of animal, but not a dog!"
?>
<?php
$answer = 0;
if(isset($_GET["answer"]))
$answer = $_GET["answer"];
switch($answer)
{
default:
echo "Which version of PHP are you using?<br /><br />";
echo "<a href=\"?answer=3\">3</a><br />";
echo "<a href=\"?answer=4\">4</a><br />";
echo "<a href=\"?answer=5\">5</a><br />";
break;
case 3:
echo "Ugh that's old, upgrade now!";
break;
case 4:
echo "Still on version 4? Give PHP 5 a try!";
break;
case 5:
echo "Good choice!";
break;
}
?>
//while loops
<?php
$i = 0;
while($i < 5)
{
echo $i."<br>";
$i++;
}
?>
//do while loops
<?php
$i = 0;
do
{
echo $i."<br>";
$i++;
} while($i < 0);
?>
//for loops
<?php
for($i = 0; $i < 5; $i++)
{
echo $i."<br />";
}
?>
//for each loops
<?php
$animals = array("Dog", "Cat", "Snake", "Tiger");
foreach($animals as $animal)
echo $animal . "<br />";
?>
<? include("includedfile.php"); ?>
<?php
function SayHello($to){
echo "Hello, ".$to;
}
SayHello("World");
?>
**4 Basic data types **
- Boolean
- Integer
- Float
- String
Complex data types
- array
- object
- resource
- NULL
<?php
$number1 = 6;
$number2 = 2;
echo "Addition: " . ($number1 + $number2);
echo "<br /><br />";
echo "Substraction: " . ($number1 - $number2);
echo "<br /><br />";
echo "Multiplication: " . ($number1 * $number2);
echo "<br /><br />";
echo "Division: " . ($number1 / $number2);
echo "<br /><br />";
?>
<?php
$number1 = "10";
$number2 = 20;
if((is_numeric($number1)) && (is_numeric($number2)))
echo "Result: " . ($number1 + $number2);
else
echo "Both variables have to be numbers!";
?>
<?php
$var = "30";
echo "The current datatype of var is: " . gettype($var) . "<br /><br />";
$var = (int)$var;
echo "The current datatype of var is: " . gettype($var);
?>
- Double quoted strings are parsed by PHP for variables and special escape sequences
echo "This is my sample test $test"; //$test evaluated
echo 'This is my sample test $test'; //$test printed variable name
//concatenating string
<?php
$newString = $string1 . $string2;
?>
//string length
<?php
$string = "Hello world!";
echo "Length of string: " . strlen($string);
?>
//lower case / upper case
<?php
$string = "Hello world!";
echo strtolower($string);
echo "<br />";
echo strtoupper($string);
?>
// find the position of string
<?php
$string = "Hello world!";
echo "Position of world in string: " . strpos($string, "world");
?>
//returning a part of string
<?php
$string = "Hello world!";
$startPos = strpos($string, "world");
$length = strlen("world");
$substring = substr($string, $startPos, $length);
echo "A specific part of the string: " . $substring;
?>
<?php
$array = array();
?>
<?php
$animals = array("Monkey", "Lion", "Turtle", "Horse");
?>
<?php
$animals = array("Monkey", "Lion", "Turtle", "Horse");
echo $animals[2];
?>
<?php
$animals = array(2 => "Monkey", "Lion", "Turtle", "Horse");
echo $animals[2];
?>
<?php
$namesAndAges = array("John Doe" => 45, "Jane Doe" => 33, "Dog Doe" => 11);
echo "The age of Jane Doe: " . $namesAndAges["Jane Doe"];
?>
<?php
$contacts = array();
$contacts["Friends"] = array("Me", "John Doe");
$contacts["Family"] = array("Mom", "Dad");
$contacts["Enemies"] = array("Stalin", "Hitler");
foreach($contacts as $categoryName => $value)
{
echo "<b>" . $categoryName . ":</b><br />";
foreach($contacts[$categoryName] as $name)
{
echo $name . "<br />";
}
echo "<br />";
}
?>
- explode -> string to array, using some saparator
- Implode -> array to string, using some saparator
<?php
$values = "Rabbit|Whale|Penguin|Bird";
$array = explode("|", $values);
print_r($array);
echo "<br /><br />";
$string = implode(" and ", $array);
echo $string;
?>
<?php
$animals = array("Dog", "Tiger", "Snake", "Goat");
if(in_array("Snake", $animals))
echo "Snake is in the array!";
else
echo "No snake in the array!";
?>
<?php
$array = array("Dog", "Tiger", "Snake", "Dog");
echo "Animals in array:<br />";
foreach($array as $value)
echo $value . "<br />";
echo "<br />";
$array = array_unique($array);
echo "Animals in unique array:<br />";
foreach($array as $value)
echo $value . "<br />";
?>
<?php
$animals = array("Dog", "Tiger", "Snake", "Goat");
$randomAnimal = $animals[array_rand($animals, 1)];
echo "Random animal: " . $randomAnimal;
?>
<?php
$animals = array("Dog", "Tiger", "Snake", "Goat", "Rabbit", "Whale", "Bird");
echo "Unsorted animals: " . implode(", ", $animals);
echo "<br /><br />";
sort($animals);
echo "Sorted animals: " . implode(", ", $animals);
echo "<br /><br />";
$animals = array_reverse($animals);
echo "Sorted animals, descending order: " . implode(", ", $animals);
?>