Saturday 6 April 2024

The anatomy of a PHP Page(or) Embedding PHP Code in Your Web Pages

A PHP script is a file (ending with a .php extension) consisting of text, HTML, and PHP instructions interspersed throughout the file. 

The PHP instructions are contained within two HTML style tags; is the closing tag. Everything between these two tags is interpreted by the PHP module (also called interpreter) and converted to regular text and HTML before being sent back to the requesting browser.

There are four delimitation variants:

  1. Default Syntax 
  2. Short-Tags 
  3. Script 
  4. ASP Style 
  5. Short-circuit syntax

Embedding Multiple Code Blocks :

Commenting Your Code: 

  1. Single-Line C++ Syntax
  2. Shell Syntax 
  3. Multiple-Line C Syntax (/*………*/)

Default Syntax 
The default delimiter syntax opens with 
<?php 
echo “ First PHP program”;  
?>

Output: First PHP program

Short-Tags:
<?
print "This is another PHP example.";
?>

Output: This is another PHP example.

Script
<script language="php">
print "This is another PHP example.";
</script>

ASP Style
<%
print "This is another PHP example.";
%> 

Short-circuit syntax:  
<?= 
"This is another PHP example.";
?>  

This is functionally equivalent to both of the following variations: 
<? 
echo "This is another PHP example.";     
?> 
<?php 
echo "This is another PHP example.";    
?>


Embedding Multiple Code Blocks:
<html>
<head>
<title><?php echo "Welcome to my web site";?></title>
</head>
<body>
<?php
$date = " April 06, 2024";
?>
<p>Today's date is <?=$date;?></p>
</body>
</html>

Commenting Your Code

1. Single-Line C++ Syntax
PHP supports C++
single-line comment syntax, which is prefaced with a double slash (//), like this:
<?php
// Title: My first PHP script
// Author: Narendra
echo "This is a PHP program.";
?>

2.Shell Syntax
PHP also supports an alternative to the C++ style single-line syntax, known as shell syntax, which is prefaced with a hash mark (#). 
<?php
# Title: My first PHP script
# Author: Narendra
echo "This is a PHP program.";
?>

3. Multiple-Line C Syntax
PHP also offers a multiple-line variant that can open and close the comment on different lines. 
Here’s an example:
<?php
/*
Title: My first PHP script
Author: Narendra
*/
echo "This is a PHP program.";
?>

Outputting Data to the Browser
The simplest of dynamic web sites will output data to the browser, and PHP offers several methods for doing so.
1. print() Statement 
2. echo() Statement
3. printf() Statement 
4. sprintf() Statement 

print() Statement
The print() statement outputs data passed to it . Its prototype looks like this:  int print(argument)
Example
<?php     
print("<b>welcome to PHP</b>");
?>

echo() Statement
<?php
echo " welcome to PHP ";
?>

printf() Statement 
The printf() statement is ideal when you want to output a blend of static text and dynamic information stored within one or several variables.
Example
<?php
printf("Bar inventory: %d bottles of tonic water.", 100);   ?>
Output:
Bar inventory: 100 bottles of tonic water.

sprintf() Statement 
The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.
Example
<?php
$cost = sprintf("$%.2f", 43.2);
echo $cost;
?> 

Output
$43.20



HTML5 Input Types

 HTML5 added several new input types:

<input type="color"/>

<input type="date"/>

<input type="datetime-local"/>

<input type="email"/>

<input type="month"/>

<input type="number"/>

<input type="range"/>

<input type="search"/>

<input type="tel"/>

<input type="time"/>

<input type="url"/>

<input type="week"/>


New Tags in HTML5

 <audio>

<bdi> Bi-Directional Isolation

<canvas> 

<datalist> 

<details> 

<dialog>

<embed>

<figcaption> tag defines a caption for a <figure> element

<figure> 

<footer> 

<header> 

<mark> 

<meter> 

<nav> tag defines a set of navigation links

<output>

<progress> 

<source> 

<video>  

<wbr> (Word Break Opportunity) 

<article> 

<aside> 

<main> 

<menuitem> 

<picture> 

<ruby> tag specifies a ruby annotation.

<rp> 

<rt>

<section> 

<summary> 

<time> 

<track> tag specifies text tracks for media elements <audio> and <video>

 

Multi page form in PHP with Example

A multi page form in PHP are used to retain values of a form and can transfer them from one page to another . 

multipage.php

<html>

<body>

<div style="width: 500px; text-align: left;">

<form action="multipage2.php" method="post">

<p>Page 1 Data Collection:</p>

<input type="hidden" name="submitted" value="yes" />

Your Name: <input type="text" name="yourname" maxlength="150" />

 <br /><br />

<input type="submit" value="Submit" style="margin-top: 10px;" />

</form>

</div>

</body>

</html>

OUTPUT







 multipage2.php

<html><body>

<div style="width: 500px; text-align: left;">

<form action="multipage3.php" method="post">

<p>Page 2 Data Collection:</p>

Selection:

<select name="yourselection">

<option value="nogo">make a selection...</option>

<option value="Choice1">Choice1</option>

<option value="Choice2">Choice2</option>

<option value="Choice3">Choice3</option>

</select><br /><br />

<input type="hidden" name="yourname" value="<?php echo $_POST['yourname']; ?>" />

<input type="submit" value="Submit" style="margin-top: 10px;" />

</form>

</div>

</body></html>

OUTPUT






multipage3.php

<html>

<body>

<div style="width: 500px; text-align: left;">

<form action="multipage4.php" method="post">

<p>Page 3 Data Collection:</p>

Your Email: <input type="text" name="youremail" maxlength="150" /><br />

<input type="hidden" name="yourname" value="<?php echo $_POST['yourname']; ?>" />

<input type="hidden" name="yourselection" value="<?php echo $_POST['yourselection']; ?>" />

<input type="submit" value="Submit" style="margin-top: 10px;" />

</form>

</div>

</body>

</html>

OUTPUT






multipage4.php


<html>

<head><title>Page 4</title></head>

<body><div style="width: 500px; text-align: left;">

<?php

//Display the results.

echo "Your Name: " . $_POST['yourname'] . "<br />";

echo "Your Selection: " . $_POST['yourselection'] . "<br />";

echo "Your Email: " . $_POST['youremail'] . "<br />";

?>

<a href="multipage.php">Try Again</a>

</div></body></html>

OUTPUT



Sunday 1 November 2020

What is Information Security?

Information Security is not only about securing information from unauthorized access. Information Security is basically the practice of preventing unauthorized access, use, disclosure, disruption, modification, inspection, recording or destruction of information. Information can be physical or electronic one. 


Monday 26 October 2020

Write a PHP program to store user registration information (like user name, address, DOB, age, Aadhar card number, gender) with data base and display that details on view screen in tabular manner

user.html 

<html>

<head>

<title> user registration information</title>

</head>

<body>

<h3 align="center">user registration information</h3>

<form name="registration" method="post" action="storeuser.php">

<table align="center" height="450">

<tr>

<td>Name:</td>

<td><input type="text" name="name" id="name" /></td>

</tr>

<tr>

<td>Address:</td>

<td><textarea rows="4" cols="30" name="address" id="address"></textarea>  </td>

</tr>

<tr>

<td>Date of Birth:</td><td><input type="date" name="dob" id="dob"/></td>

</tr>

<tr>

<td>Age:</td>

<td><input type="number" name="age" id="age" /></td>

</tr>

<tr>

<td>Aadhar Number:</td>

<td><input type="text" name="aadhar" id="aadhar" /></td>

</tr>

<tr>

<td>Gender:</td>

<td><input type="radio" name="gender" value="male" /> Male

<input type="radio" name="gender" value="female" /> Female</td>

</tr>

<tr>

<td colspan="2" align="center">

<input value="Submit" type="submit"/>

<input value="reset" type="reset" />

</td>

</tr>

</table>

</body>

</html>



storeuser.php

<?php

$link = mysqli_connect("localhost", "root", "", "wit");

// Check connection

if($link === false){

    die("ERROR: Could not connect. " . mysqli_connect_error());

}

// Escape user inputs for security

$user = mysqli_real_escape_string($link, $_POST['name']);

$address = mysqli_real_escape_string($link, $_POST['address']);

$dob = mysqli_real_escape_string($link, $_POST['dob']);

$age = mysqli_real_escape_string($link, $_POST['age']);

$aadhar = mysqli_real_escape_string($link, $_POST['aadhar']);

$gender = mysqli_real_escape_string($link, $_POST['gender']);

// attempt insert query execution

$sql = "INSERT INTO storeuser (name, address,dob,age,aadhar,gender) VALUES ('$user', '$address',

'$dob','$age','$aadhar','$gender')";

if(mysqli_query($link, $sql)){

    echo "Records added successfully.";

$link_address="store.html";

echo "<a href='".$link_address."'>Go to Register Page</a>";

} else{

    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}

// close connection

mysqli_close($link);

?>

<a href="display.php">|Display|</a>

display.php

<html>

<body>

<?php

mysql_connect("localhost","root","");

mysql_select_db("wit");

$r=mysql_query("select * from storeuser");

echo"<table border=1>";

echo"<tr><th>User Name</th><th>Address</th><th>Date of Birth</th><th>Age</th><th>Aadhar Number</th><th>Gender</th></tr>";


while ($row=mysql_fetch_array($r))

{        echo"<tr>";

      echo "<td>".$row['0']."</td>"."<td>".$row['1']."</td>"."<td>".$row['2']."</td>"."<td>".$row['3']."</td>"."<td>".$row['4']."</td>"."<td>".$row['5']."</td>";

        echo "</tr>";

}

echo"</table>";

?>

<a href="store.html">|Home|</a>

</body>

</html>



Monday 19 October 2020

Write a script that asks the user to enter two numbers and outputs text that displays the sum, product, difference and quotient of the two numbers.

 <html>

<head>

<title>javascript using sum, product, difference and quotient</title>

<script>

     var number1; 

     var number2; 

     var sum; 

     var product; 

     var difference; 

     var quotient;

     number1 = parseInt(window.prompt( "Enter first number" ));

     number2 = parseInt(window.prompt( "Enter second number" ));

     sum = number1 + number2; 

     product = number1 * number2; 

     difference = number1 - number2; 

     quotient = number1 / number2;

document.writeln( "<h3>The sum is " + sum + "</h3>" );

document.writeln( "<h3>The product is " + product + "<h3>");

document.writeln( "<h3>The difference is " + difference + "<h3>");

document.writeln( "<h3>The quotient is " + quotient + "<h3>");

</script></head>

</html>

Output:



Sunday 18 October 2020

Design a form using HTML and java script to collect the details from the user such as name, gender (radio button), address, state & city. Create a dynamic drop down list for state and based on state, create another drop down list containing list of cities for that state.

 <html>

<head><title>dynamic drop down list</title>

<script>

function dropdownlist()

{

var state=document.getElementById('input').value;

if(state==="AP")

{

var array=["Tirupati","vizag","kadapa","Amaravathi","kurnool"];

}

else if(state==="KA")

{

var array=["Bangalore","Mysore","Mangalore","Hubli"];

}

else if(state==="TN")

{

var array=["vellore","chennai","Coimbatore","Madurai"];

}

else if(state==="TG")

{

var array=["Hyderabad","Warangal","Nizamabad","Karimnagar"];

}

else

{

var array=[];   

}

var string='';

for(i=0;i<array.length;i++)

{

string=string+"<option>"+array[i]+"</option>";

}

string="<select name='loc'>"+string+"</select>";

document.getElementById('output').innerHTML=string;

}

</script></head>

<body>

<form name="collectdetails">

<p>Name:<input type="text" name="uname" id="uname" /></p>

<p>Gender:<input type="radio" name="gender" value="male">Male

<input type="radio" name="gender" value="female">Female</p>

<p>Address:<textarea rows="4" cols="10" placeholder="please enter the proper address"></textarea></p>

<p>

State:

<select id="input" onChange='dropdownlist()'>

<option>select</option>

<option>AP</option>

<option>TG</option>

<option>TN</option>

<option>KA</option>

</select>

</p>

City:

<div id="output">

<select>

<option>select</option>

</select>

</div></form></body>

</html>

Output:



Write a script that reads an integer and determines whether it is arm strong number or not.

     <html><head><title>Armstrong number or not</title>

    <script>

function Armstrong()

{

var flag,number,remainder,addition = 0;

number = Number(document.getElementById("num").value);

flag = number;

while(number > 0)

{

remainder = number%10;

addition = addition + remainder*remainder*remainder;

number = parseInt(number/10);

}

if(addition == flag)

{

window.alert("The input number is Armstrong");

}

else

{

window.alert("The input number is not Armstrong");

}

}

    </script></head>

<body>

<h1>Whether a number is Armstrong or not</h1>

    Enter the number :<input type="text" name="n" id = "num"/><br/>

<input type="submit" value="CHECK" onClick="Armstrong()" />

</body>

    </html>

For example Armstrong numbers are : 153    370     371     407

Output: 




Write a PHP script to sort the elements in descending order of an array.

 Example 1

<?php 

$numbers = array(20, 91, 4, 39, 23); 

rsort($numbers); 

$arraylength = count($numbers); 

for($x = 0; $x < $arraylength; $x++) { 

    echo $numbers[$x]; 

    echo "<br>"; 

?> 

Output: 

91

39        

23        

20        

4

Example 2

<?php

$cars = array("Maruthi", "TATA", "KIA");

rsort($cars);

$carslength = count($cars);

for($i = 0; $i < $carslength; $i++) {

  echo $cars[$i];

  echo "<br>";

}

?>

Output: 

TATA
Maruthi
KIA

Example 3

<?php

// Define array

$numbers = array(1,2,3,4,5,6,7,8,9,10);

// Sorting and printing array

rsort($numbers); // rsort- sort arrays in descending order

print_r($numbers);

?>

Output: 

Array ( [0] => 10 [1] => 9 [2] => 8 [3] => 7 [4] => 6 [5] => 5 [6] => 4 [7] => 3 [8] => 2 [9] => 1 )

Example 4

<?php

$colors = array("Red", "Blue", "Green","Yellow");

rsort($colors);

print_r($colors);

?>

Output: Array ( [0] => Yellow [1] => Red [2] => Green [3] => Blue )



Write a PHP program that receives the value of N using HTML form and displays the first N Fibonacci numbers as HTML list.

  <?php

function Fibonacci($num){ 

if ($num == 0) 

return 0;     

else if ($num == 1) 

return 1;     

else

return (Fibonacci($num-1) +  Fibonacci($num-2)); 

?>

<html>

<head><title>Fibonacci example</title></head>

<body><form action="" method="POST">

Enter Number:<input type="text" name="num"/><br/><br/>

<input type="submit" name="print" value="Print N Fibonacci Numbers"/>

</form>

<?php   

if(isset($_POST["print"]))

{

$num = $_POST["num"]; 

for ($counter = 0; $counter < $num; $counter++){   

echo "  ".Fibonacci($counter)."  "; 

}

}

?>

</body></html>