Vous êtes sur la page 1sur 17

1.

Introduction to PHP :
<?php echo Welcome User; ?>

2. Datatypes:
2.1 Strings: <?php $a = "Welcome User!"; echo $a; echo "<br>"; $b = To the world of PHP; echo $b; ?> 2.2 Integers: <?php $a = 1234; // decimal number $a = -123; // negative number ?> 2.3 Floating Point Numbers: <?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ?>

2.4 Booleans: $x=true; $y=false;

2.5 Arrays: <?php $fruits=array("Apple","Orange","Mango"); ?> 2.6 Objects: <?php class Shoes { var $color; function Shoes($color="green") { $this->color = $color; } function what_color() { return $this->color; } } ?> 2.7 Null Value <?php $a="Hello world!"; $a=null; ?>

3. Variables:
<?php $word="Hello world!"; $a=5; $b=10.5; ?> Local and Global variable scope: <?php $a=10; // global scope function new() { $b=20; // local scope echo "<p>Test variables inside the function:<p>"; echo "Variable a is: $a"; echo "<br>"; echo "Variable b is: $b"; } new(); echo "<p>Test variables outside the function:<p>"; echo "Variable a is: $a"; echo "<br>"; echo "Variable b is: $b"; ?> Static keyword: <?php function new() { static $a=0; echo $a; $a++;

} new(); new(); new(); ?>

4. Constants:
<?php define("CONSTANT", "Hello world."); echo CONSTANT; ?>

5. Operators:
Arithmetic Operators: <?php $x=10; $y=6; echo ($x + $y); // outputs 16 echo ($x - $y); // outputs 4 echo ($x * $y); // outputs 60 echo ($x / $y); // outputs 1.6666666666667 echo ($x % $y); // outputs 4 ?> String Operators: <?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello "; $a .= "World!"; ?>

// now $a contains "Hello World!"

Increment/ Decrement Operators: <?php $x=10; echo ++$x; // outputs 11 $y=10; echo $y++; // outputs 10 $z=5; echo --$z; // outputs 4 $i=5; echo $i--; // outputs 5 ?>

6. Conditional Statements:
if Statement: <?php $a="10"; $b="20"; if ($a<$b) { echo $a."is smaller than".$b; } ?>

if..else statement:

<?php $a="20"; $b="10"; if ($a > $b) { echo "a is greater than b"; } else { echo "a is NOT greater than b"; } ?>

elseif statement: <?php $a="10"; $b="20"; if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } ?>

while statement: <?php $i = 1; while ($i <= 10): echo $i; $i++; end while; ?> Do while: <?php $i = 0; do { echo $i; } while ($i > 0); ?> Switch Case: <?php switch ($i) { case "apple": echo "i is apple"; break; case "cream": echo "i is cream"; break; case "cake":

echo "i is cake"; break; } ?> For Loop: <?php for ($i = 1; $i <= 10; $i++) { echo $i; }

7. Arrays:
<?php $fruits=array("Banana","Kiwi","Mango"); ?> Indexed Arrays: <?php $fruits=array("Banana","Kiwi","Mango"); echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . "."; ?> Associative Arrays: <?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?> Multidimensional Arrays: <?php $fruits = array ( array("Apple",100,96),

array("Orange",60,59), array("Blueberry",110,100) ); ?> Array Functions: Sort() <?php $fruits=array("Apple","Orange","Kiwi"); sort($fruits); ?> array_slice() <?php $a=array("red","green","blue","yellow","brown"); print_r(array_slice($a,2)); ?> array_push() <?php $a=array("red","green"); array_push($a,"blue","yellow"); print_r($a); ?> array_pop() <?php $a=array("red","green","blue"); array_pop($a); print_r($a); ?> array_count_values() <?php $a=array("A","Cat","Dog","A","Dog");

print_r(array_count_values($a)); ?>

8. PHP FUNCTIONS:
User Defined functions: <?php function Msg() { echo "Hello world!"; } Msg(); ?> Use of Return: <?php function square($num) { return $num * $num; } echo square(4); ?> Built-In Functions: <?php if (function_exists('imap_open')) { echo "IMAP functions are available.<br />\n"; } else { echo "IMAP functions are not available.<br />\n"; } ?>

UNIT-III GET METHOD: <html> <body> <form action="getmethod.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html> Fetching the data submitted: <html> <body> Welcome <?php echo $_GET["name"]; ?><br> Your email address is: <?php echo $_GET["email"]; ?> </body> </html> POST METHOD: <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>

Fetching the data submitted: <html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>

COOKIES:
Creating a cookie: <?php setcookie("user", "Anurag", time()+3600); ?> Retrieving a cookie value: <html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br>"; else echo "Welcome guest!<br>"; ?> </body>

</html> Deleting a cookie: <?php setcookie("user", "", time()-3600); ?>

SESSIONS:
Creating a session: <?php session_start(); ?> <html> <body> </body> </html> Storing a seesion value: <?php session_start(); $_SESSION['views']=1; ?> <html> <body>

<?php echo "Pageviews=". $_SESSION['views']; ?> </body> </html> To view the count of people visiting the site: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo "Views=". $_SESSION['views']; ?> Destroying a session by using: (a) unset function: <?php session_start(); if(isset($_SESSION['views'])) unset($_SESSION['views']); ?> (b) session_destroy function: <?php session_destroy(); ?>

UNIT-IV
Opening a connection in MySQL: <?php // Create connection through localhost $con=mysql_connect("localhost","anurag","abc123","my_db"); // Check connection if (mysql_connect_error($con)) { echo "Failed to connect to MySQL: " . mysql_connect_error(); } ?> Creating a Database: <?php

$sql="CREATE DATABASE my_db"; if (mysql_query($con,$sql)) { echo "Database my_db created successfully"; } else { echo "Error creating database: " . mysql_error($con); } ?> Creating a Table: <?php $sql=CREATE TABLE IF NOT EXISTS `brand` ( `B_id` varchar(10) NOT NULL, `B_name` varchar(40) NOT NULL ); if (mysql_query($con,$sql)) { echo "Table brand created successfully"; } else { echo "Error creating table: " . mysql_error($con); } ?>

Inserting values in a table: <?php mysql_query=$con,INSERT INTO `brand` (`B_id`, `B_name`) VALUES ('1', 'Apple'), ('2', 'Samsung'), ('3', 'Nokia'), ('4', 'Sony'), ('5', 'LG'); mysql_close($con); ?>

Vous aimerez peut-être aussi