1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
$text = "Hello World";
if ( preg_match( "/r.*?d/", $text, $array ) ) {
print "<pre>\n";
print_r( $array );
print "</pre>\n";
}
?>
Output :
Array
(
[0] => rld
)
Get an IP address
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
$test = "126.122.95.52";
if ( preg_match( "/(\d+)\.(\d+)\.(\d+)\.(\d+)/", $test, $arr ) ) {
print "<pre>\n";
print_r( $arr );
print "</pre>\n";
}
?>
Output :
Array
(
[0] => 126.122.95.52
[1] => 126
[2] => 122
[3] => 95
[4] => 52
)
1 2 3 4 5 6 7 8 9 10 11
<?php
$arr = array('one', 2, 'three');
$arr[] = 'the fourth element';
echo $arr[3]."<br>";
echo $arr[1];
?>
Output :
the fourth element
2
Array indexing
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php $arr1 = array( "Jan ", "Feb ", "Mar " ); $arr2 = array( "11 ", "12 ", "13 " ); $arr3 = array( "2001", "2002", "2003" ); echo( $arr1[1] . $arr2[0] . $arr3[1] ); ?>
Output :
Feb 11 2002
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php $arr[0] = 32; $arr[1] = 52.15; $arr[2] = $arr[0] + $arr[1]; echo "Result = "."$arr[0] + $arr[1] = $arr[2]"; ?>
Output :
Result = 32 + 52.15 = 84.15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
$num['A'] = 12;
$num['A']++;
$num['B'] = 3;
$num['total'] = $num['A'] + $num['B'];
if ($num['total'] > 15) {
print "Greater Than 15.<br>";
}
print 'Number is : ' . $num['A'];
?>
Output :
Greater Than 15.
Number is : 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php
$arr[] = "PHP";
$arr[] = "C";
$arr[] = "C++";
$arr[] = "Java";
$arr[] = "Perl";
$arr[] = "Python";
$arr[] = "Ruby";
foreach($arr as $lang)
{
if(!strcmp($lang, "PHP")){
print("<b>" . $lang . "</b> <br />");
}else{
print($lang . " <br />");
}
}
?>
Output :
PHP
C
C++
Java
Perl
Python
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<?php
class Cat {
var $age;
function Cat($new_age){
$this->age = $new_age;
}
function Birthday( ){
$this->age++;
}
}
$a = new Cat(1);
echo "Age is $a->age <br />";
echo "Birthday<br/>";
$a->Birthday( );
echo "Age is $a->age <br />";
?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php
class Item {
var $name;
function Item( $name="") {
$this->name = $name;
}
function setName( $n) {
$this->name = $n;
}
function getName () {
return $this->name;
}
}
$item = new Item("5");
print $item->getName ();
?>
Output :
5
Invoking parent constructors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php
class Staff
{
function __construct()
{
echo "<p>Staff constructor called!</p>";
}
}
class Manager extends Staff
{
function __construct()
{
parent::__construct();
echo "<p>Manager constructor called!</p>";
}
}
$employee = new Manager();
?>
Output :
Staff constructor called!
Manager constructor called!
Using Default Constructors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php
class Dog {
function __construct($name='No-name', $breed='breed unknown', $price = 23) {
$this->name = $name;
$this->breed = $breed;
$this->price = $price;
}
}
$aDog = new Dog();
$tweety = new Dog('A', 'a');
printf("<p>%s is a %s and costs \$%.2f.</p>\n",
$aDog->name, $aDog->breed, $aDog->price);
$tweety->price = 34.56;
printf("<p>%s is a %s and costs \$%.2f.</p>\n",
$tweety->name, $tweety->breed, $tweety->price);
?>
Output :
No-name is a breed unknown and costs $23.00.
A is a a and costs $34.56.
1 2 3 4 5 6 7 8 9 10 11
<?php session_start(); ($_SESSION['count']) ? $_SESSION['count']++ : $_SESSION['count'] = 1; ?> You have been stay here <?php echo( $_SESSION['count'] ); ?> times in this session
All the PHP code will execute on the server before the page is sent to the client, meaning all of the PHP calls have all been replaced by whatever text they returned once the JavaScript is able to execute.
1 2 3 4 5
<script>
document.write("<?php echo 'hello!'; ?>");
</script>
1 2 3 4 5 6 7 8 9
<?php echo "<script language=\"JavaScript\">"; echo "alert(\"Hello World\");"; echo "</script>"; ?>
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php $phpvar=1; ?> <script type="text/javascript"> jsvar = 3; //Assign jsvar javascript variable value to php variable $phpvar '<?php $phpvar="<script>document.write(jsvar)</script>"; ?>'; </script> <?php echo $phpvar; ?>
‘Loop’ Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<script type="text/javascript">
var counter = 0;
for(i=0;i<10;i++){
counter++;
}
</script>
<?php
$phpvar= "<script>document.write(counter)</script>";
echo $phpvar;
?>
‘Session’ Example
1 2 3 4 5 6 7 8 9
<?php session_start(); $_SESSION['nume']='Sanat';?>
<!-- HTML code -->
<script type="text/javascript">
alert("Welcome <?php echo $_SESSION['nume']; ?>");
</script>
‘Switch’ Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="<?php
$w="do";
switch($w){
case 'do': echo "You will be logged in?"; break;
case 'loguot': echo "You will be logged out?" ; break;
}
?>";
}
</script>
<a href="" onclick="test(); return false;"> test </a> <br>
<span id="php_code"> </span>
Pass the value within url.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
<?php
if(isset($_GET[action])){
// Retrieve the GET parameters and executes the function
$funcName = $_GET[action];
$vars = $_GET[vars];
$funcName($vars);
} else if (isset($_POST[action])){
// Retrieve the POST parameters and executes the function
$funcName = $_POST[action];
$vars = $_POST[vars];
$funcName($vars);
} else {
echo "<INPUT NAME='click_me' TYPE='button' ONCLICK='javascript:javaFunction()' VALUE='Click Me'>";
}
function phpFunction($v1){
// makes an array from the passed variable
// with explode you can make an array from string.
$varArray = explode(",", $v1);
echo "First Value: $varArray[0] <BR>";
echo "Second Value: $varArray[1]<BR>";
}
?>
<SCRIPT language="javascript">
function javaFunction(){
// In the varArray are all the variables you want to give with the function
var varArray = new Array();
varArray[0] = "Hello";
varArray[1] = "World";
// the url which you have to pass an action to the GET- or POST-variable
var url="<?php echo $_SERVER[PHP_SELF];?>?action=phpFunction&vars="+varArray;
// Opens the url in the same window
window.open(url, "_self");
}
</SCRIPT>
Output :
First Value: Hello
Second Value: World
Copyright © 2010 PHP code. net. All Rights Reserved. Designed By: Web Design Company