Nov 17, 2011

PHP EXAMPLES

Sample Date and Time in PHP


To print the date and time on screen use the PHP date () function. We can handle the format to display by passing it as the first argument

Here is an example
It is too simple, and can handle the format as eg. Day / Month / Year. O Day-Month-Year, the type of numbers, for example, January = 1, January = 01. Show the month name directly.Even though he would need to translate it. Since the language is generally used on the server


[Code] <html>
<body>
The date today is: <? = Date ("d / m / Y");?>.
<br />
The server's local time is: <? = Date ("G: i: s");?>.
</ Body>
</ Html> [/ code]







Read Sample contents of a file

In this example we will see how to save to a variable content of any other file and display it.

[Code] <? Php
$ Text = file_get_contents ("texto.txt") / / read and store the file in $ text texto.txt
$ Text = nl2br ($ text); / / replace with a new $ text $ text, but change the line breaks () for a line break in html (br)
echo $ text; / / show the text
?>
[/ Code]
The truth is quite simple, and using file_get_contents, we could read different types of files and process them in different ways ...






Sample text replacement by another



A simple example of how to replace a text by another in PHP ...

[Code]
<? Php
$ Text = "I have two sweets. Also I have three lollipops, but I want to eat the two candies. And I would buy two gum";
$ Variable = str_replace ("two", "four", $ text);
echo $ variable;
# The end result would be: "I have four candy. Also I have three lollipops, but I want to eat four candy. And I would buy four gum."
?>
[/ Code]



Back Button Example in PHP

Many times we need to put a button to return to where they were our customers, there are different ways to make the example presented below is written in PHP:
<? Php Echo '<a href="'.$_SERVER['HTTP_REFERER'].'">Back Button Example in PHP </ a>';
?>
This will take a page to which we come.

While Loop Example in PHP

The PHP while loop is used to run a piece of code while the condition remains true. Generally counters are typically used for, or to display records from a database.
In this example, the loop ends when the counter reaches 10.We will do this using a variable called $ i, which is usually identified as an accountant

[Code]
<?
# EjemploDe.COM
# While Loop Example in PHP
$ I = 0; / / Inializamos the variable to 0
while ($ i == 10) {
$ I + +; / / increase $ i by one
echo "i equals now." $ i. "</ br>" / / show text
}
echo "End of the while loop. The while loop ends in". $ i. "";
?>
[/ Code]This example displays the following[Code]
i now equals 1
i now equals 2
i now equals 3
i now equals 4
i now equals 5
i now equals 6
i now equals 7
i now equals 8
i now equals 9
i now equals 10
End of while loop. The while loop completed in 10
[/ Code]


 Example of Calculations in PHP


It works very simple, just set the two variables, $ $ SegundoValor PrimerValor and now are 10 and 30.
<?php # Comentarios. # Ejemplo de programacion en PHP simple # Haciendo simples calculos entre variables predefinidas # Por EjemploDe.Com 
$PrimerValor = 10; //Comentario: Edita el numero para cambiar el primer valor $SegundoValor = 30; //Comentario: Edita el numero para cambiar el segundo valor 
//Seccion de calculos $suma = $PrimerValor + $SegundoValor; //Sumamos $resta = $PrimerValor - $SegundoValor; //Restamos $multiplicacion = $PrimerValor * $SegundoValor; //Multiplicamos $division = $PrimerValor / $SegundoValor; //Dividimos 
//A continuacion mostramos las cuentas en un formato textual 
echo "<h1>Formato Textual</h1>"; echo "La <strong>suma</strong> entre <strong>".$PrimerValor."</strong> y <?php
# Comentarios.
# Ejemplo de programacion en PHP simple
# Haciendo simples calculos entre variables predefinidas
# Por EjemploDe.Com

$PrimerValor = 10; //Comentario: Edita el numero para cambiar el primer valor
$SegundoValor = 30; //Comentario: Edita el numero para cambiar el segundo valor

//Seccion de calculos
$suma = $PrimerValor + $SegundoValor; //Sumamos
$resta = $PrimerValor - $SegundoValor; //Restamos
$multiplicacion = $PrimerValor * $SegundoValor; //Multiplicamos
$division = $PrimerValor / $SegundoValor; //Dividimos

//A continuacion mostramos las cuentas en un formato textual

echo "<h1>Formato Textual</h1>";
echo "La <strong>suma</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>";
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y
# Comentarios. # Ejemplo de programacion en PHP simple # Haciendo simples calculos entre variables predefinidas # Por EjemploDe.Com 
$PrimerValor = 10; //Comentario: Edita el numero para cambiar el primer valor $SegundoValor = 30; //Comentario: Edita el numero para cambiar el segundo valor 
//Seccion de calculos $suma = $PrimerValor + $SegundoValor; //Sumamos $resta = $PrimerValor - $SegundoValor; //Restamos $multiplicacion = $PrimerValor * $SegundoValor; //Multiplicamos $division = $PrimerValor / $SegundoValor; //Dividimos 
//A continuacion mostramos las cuentas en un formato textual 
echo "<h1>Formato Textual</h1>"; echo "La <strong>suma</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>"; echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>";
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>";
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>";
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>"; echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>";
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>";
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>";
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>"; echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>";
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y 
".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>";
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$division."</strong><br/>"; //A continuacion mostramos las cuentas en un formato matematico 


echo "<h1>Formato Matematico</h1>";
echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>";
echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>";
echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>";
echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>";
?>
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$division."</strong><br/>"; //A continuacion mostramos las cuentas en un formato matematico 
echo "<h1>Formato Matematico</h1>"; echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>"; echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>"; echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>"; echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>"; ?> ".$SegundoValor."</strong> es de <strong>".$division."</strong><br/>"; //A continuacion mostramos las cuentas en un formato matematico

echo "<h1>Formato Matematico</h1>";
echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>";
echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>";
echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>";
echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>";
?>

echo "<h1>Formato Matematico</h1>"; echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>"; echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>"; echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>"; echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>"; ?>
This example show the following in PHP
The sum of 10 and 30 is 40
The subtraction between 10 and 30 is -20
The multiplication between 10 and 30 is 300
The division between 10 and 30 is 0.333333333333

10 + 30 = 40
10 to 30 = -20
10 * 30 = 300
10 / 30 = 0.333333333333 






Sample PHP Class

class is part of Object-Oriented Programming (OOP, or English OOP) in PHP. A class is an object.
There are doing a Sample Class in PHP.
File: main.class.php 
[Code] <?
# File Class
WordClass class {
var $ text;
function __construct ($ number) {
echo "start class". $ number. ""
}
cambiarWord function ($ nuevotexto) {
echo "text". $ this-> text. "has been replaced by:";
$ This-> text = $ nuevotexto;
echo $ this-> text;
echo "<br/>";
}
}
?> [/ Code] 

File: index.php 
[Code] <?
# Main file where the class call
include ("main.class.php);
$ Chatter = new WordClass (1) / / show "to start the class: 1" Since we number 1 as a parameter
$ Mumbo-> cambiarWord ("Hello as you go");
$ Mumbo-> cambiarWord ("Lalala");
?> [/ Code]

And more later....

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes