Simple PHP examples
Written by: maffelu , 2009-10-05 08:38:08
<?php
$myName = "Maffelu";
echo "<html>";
echo "<b>".$myName."</b>";
echo "</html>";
?>
<?php
$myName = "Maffelu";
?>
<html>
<b><?php echo $myName; ?></b>
</html>
<?php
$myBool = true; //Boolean
$myInt = 10; //Integer
$myString = "Hello!" //String
$myString2 = 'Hello!' //String
?>
<?php
$myVar = "Hello";
$myVar .= " ";
$myVar .= "World!";
echo $myVar;
//Hello World!
?>
<?php
$myInt = 10; //10
$myInt -= 3; //7
$myInt += 1; //8
$myInt /= 2; //4
$myInt *= 3; //12
?>
<?php
$myArray = array();
$myArray[0] = 1;
$myArray[1] = 2;
$myArray[2] = 3;
?>
<?php
$myArray = array();
$myArray["Name"] = "Magnus";
$myArray["Age"] = "25";
$myArray["Homepage"] = "http://www.morkalork.com";
?>
<?php
//First
$myArray = array();
$myArray[0] = 0;
$myArray[1] = 1;
$myArray[2] = 2;
//Second
$myOtherArray = array();
$myOtherArray[0] = "Car";
$myOtherArray[1] = "Boat";
$myOtherArray[2] = "Airplane";
//Third
$myArrays = array();
$myArrays[0] = $myArray; //Add the first one
$myArrays[1] = $myOtherArray; //Add the second one
?>
<?php
//Standard
$myArray = array(1, 2, 3, 4, 5);
//With keys
$myKeyArray = array(0 => "car", 1 => "boat", 2 => "airplane");
$myKeyArrayAss = array("Title" => "Bilbo", "Author" => "J.R.R. Tolkien", "Rating" => "Ok");
?>
<?php
echo "Hello world!";
print "Hello world!";
echo("Hello world!");
print("Hello world!");
$echoVar = "Hello world!";
$printVar = "Hello world!";
echo $echoVar;
echo $printVar;
?>
<?php
echo "This"." is "."a"." string "."<br />";
echo "This", " is ", "a", " string ", "<br />";
?>
<?php
$myArray["Name"] = "Magnus";
$myArray["Age"] = 25;
$myArray["Homepage"] = "www.morkalork.com";
echo "<pre>";
print_r($myArray);
echo "<pre>";
?>
Array
(
[Name] => Magnus
[Age] => 25
[Homepage] => www.morkalork.com
)
<?php
//Show POST array
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<!--Show form-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="txtName" /></td>
</tr>
<tr>
<td>Message</td>
<td><textarea name="txtMessage"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" /></td>
</tr>
</table>
</form>
?>
Array
(
[txtName] => Magnus
[txtMessage] => This is my message!
[submit] => Submit
)
<?php
$id = $_GET['id'];
?>
<?php
$id = $_GET['id'];
$page = $_GET['page'];
?>
<?php
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>
Array
(
[id] => 3
[page] => 6
[user] => maffelu
)
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.