Learn Php by Aryadrj

Learn Php in simple way by Aryadrj

#Index

1.How to run Php
2.Hello world program
3.Swap the two number
4.Print the table of number
5.Creating the Simple form with button
6.Creating Form and header with Html
7.Php with html
8.Read a text file through php
9.Write data to text file through php
10.if-else example with html and changing the backgroud color       to green
11. Simple Calculator using php (if,if-else,switch is used)
12.Wisdom subscription form(Html program)
13.Displaying and tracking the visitors in webpage
14.Simple php program to interact with sql
=================================
1.How to run php

  • You need xampp software.
  • and click on start on Apache
  • now open the notepad or notepad++ and save it as filename.php on desktop
  • now cut the file and move the file in (c: drive ->xampp->htdoc) folder
  • open browser and type http://localhost/filename.php you will see the output.
===============================
2.Hello world program 

Program:

<?php
echo "hello aryadrj";
 ?>

Explanation:
  • program always start with <?php and end with ?>
  • echo is used to output the data on the screen or you can use print too.
  • Every statement end with semicolon 
===============================
3.Swap the two number

Program:

<?php
/* Program to swap the two number */
$a=10;
$b=30;
echo "Value of a: $a </br>";
echo "Value of b: $b </br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "After Swapping a=$a and b=$b";
?>

Explanation:
  • Comment line start with /*  and end with */.
  • $-> used to create variable same like perl
  • </br> is used to break the line same like /n

================================

4.Print the table of number


Program:

<?php
define('x',2);
for($i=1;$i<=10;$i++)
{
echo $i*x;
echo "</br>";
}
?>
  • Output->
2
4
6
8
10
12
14
16
18
20
===============================
5.Creating the simple form->

Program:

<!DOCTYPE HTML>
<html>  
<body>

Name:<input type="text" > 
<input type="submit">


</body>
</html>

  • Output->
  • This program is based on HTML Program
  • Html program start with <html> and end with </html>
  • every code should be written in <body> and </body>
================================

6.Creating Form and header with Html

<!DOCTYPE HTML>
<html>  
<!=====I am header======= ->

<h1 style="color:Red">

Aryadrj Saviour Group <br> Registration Form
</h1>

<body>


<!=====I am Form======== ->

Name:    <input type="text" > 
<br>
Email:    <input type="text">
<br>
Phone:    <input type="text">
<br>
Address: <input type="text"><br> <br>

<! ====I am submit button==== ->

<input type="submit">


</body>

</html>



  • Output->
  • In html the comment line start with <!   and end with ->
  • Header will start with <h1> and end with </h1> 
  • And you can give <h1 style="color:red">  in <h1> to change the color
=================================
7. Php with html

<!DOCTYPE>  
<html>  
<body>  
<?php  
echo "<h2>Hello First PHP</h2>";  
?>  
</body>  
</html>  

================================

8.Read a text file through php

program->

<?php
echo readfile("arya1.txt");
?>
  • First save the text file in the folder htdocs.
Or

Program->

<?php    
$fp = fopen("c:\\xampp\\htdocs\\arya1.txt", "r");   
echo fgets($fp);  
fclose($fp);  
?>    
  • Or you can give c:\\arya.txt but make sure that file is present in the c drive.


=================================
9.Write data to text file through php

  • Program->


<?php  
$fp = fopen('arya.txt', 'w');
fwrite($fp, 'welcome ');  
fwrite($fp, 'to php file write');  
fclose($fp);  
  echo "File written successfully";  
?>  


  • Don't forget the semicolon otherwise you will get parse error.
  • If you run this code again then old data will delete and new will be written

=================================

10.if-else example with html and changing the backgroud color       to green

<html>
<h1 style="color:red">
Aryadrj if else example
</h1>
<body bgcolor='green'>
<?php
$a=10;
if($a>100)
{
echo "Output: </br>num is less than 100";
}
else
{
echo "Output: </br>num is greater than 100";
}

?>

</body>
</html>



  • Output->
  • body< bgcolor='green'> is used to change the  background color
  • <h1 style="color:red"> is used to change the header color



=================================
11. Simple Calculator using php and html
<?php
ini_set('display_errors',0);
if(isset($_POST['submit'])) 
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];
if(is_numeric['$num1'] && is_numeric['$num2']) 
{
if(isset($_POST['g'])) 
{
$operation=$_POST['g'];
switch($operation)
{
case '+':
$result=$num1+$num2;
break; //if you dont give this then other case will also execute
case '-':
$result=$num1- $num2;
break;
case '*':
$result=$num1* $num2;
break;
case '/':
$result=$num1 /$num2;
break;
}//switch
}//if3
else
{
echo "please select the operation";
}
}//if2
else
{
echo "please enter numeric value only";
}
}

?>
<html>
<h1 style="color:Green">
AryaDrj Calculator
</h1>
<body bgcolor="Pink"> 
<form method="post" action="hello.php">
Enter 1st number:<input type="text" name="number1" > </br>
Enter 2nd number:<input type="text" name="number2"> </br>
Select Operation: </br></br>
<input type="radio" name="g" value="+"> Add </br>
<input type="radio" name="g" value="-"> Sub </br>
<input type="radio" name="g" value="*"> Mul </br>
<input type="radio" name="g" value="/"> Div </br>
<input type="submit" name="submit"></br></br>
<h2 style="color:blue"> Output:<?php echo $result;  ?> </h2>
</form>
</body>
</html>



  • Output->

==========================

12.Wisdom subscription form(html program)
<!DOCTYPE html>
<html>
<body bgcolor="gold" font-color="red">
<table align='center'>
<tr><td><h1>Wisdom Subscription form </h1></td></tr>
<tr><td align="center">Please Fill all details </td></tr>
<!-----Name---->

<table align='center'>

<tr>
<td align='center'>First name:</td>
<td><input type='text' name='name' required>*</td>
</tr>

<tr> <td> &nbsp; </td> </tr>


<tr>

<td align='center'>Last name:</td>
<td><input type='text' name='name' required>*</td>
</tr>

<tr> <td>&nbsp;</td> </tr>


<tr>

<td align='center'>Gender:</td>
<td><input type='radio' name='name'>male</td> 
<td><input type='radio' name='name'>female</td>
</tr>

<tr> <td>&nbsp;</td> </tr>

<tr>
<td align='center'>Address:</td>
<td colspan="2"> <input type='text' name='name' required>*</td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
<td align='center'>Phone:</td>
<td><input type='text' name='name' required>*</td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
<td align='center'>city:</td>
<td> <form action=" ">
<p>
<select name="electives">
<option>Bengaluru</option>
<option>Chennai</option>
<option>Hydrabad</option>
</select>
</p>
</form>
</td>
</tr>
<tr> <td>&nbsp;</td> </tr>
<tr>
<td align='center'>Payment:</td>
<td><input type='radio' name='zip'> COD</td>
<td><input type='radio' name='zip'> Online</td>
</tr>
<tr> <td>&nbsp;</td> </tr>

<tr> 

<td align='center'>Period of subscription:</td>
<td><input type='radio' name='name'>1 year</td> 
<td><input type='radio' name='name'>3 year</td>
</tr>

<table align='center'>

<tr>
<td align='center'>
<input type='submit' name='REGISTER' value="Subscribe"></td>
</tr>
</table>
</table>
</table>
</body>

</html>


output->








13.Displaying and tracking the visitors in webpage
<?php
print"<h1>REFRESH PAGE</h1>";
$name="new.txt";

$myfile=fopen($name,"r") or die("unable");
$txt1=fgets($myfile);
$txt1=intval($txt1);
fclose($myfile);
$txt1++;
echo "result read=",$txt1;
$myfile=fopen($name,"w") or die("unable");
fwrite($myfile,$txt1);
fclose($myfile);
?>

=============================

14.Simple php program to interact with sql


<html>

<head>
<style>
table, td, th
{
 border: 1px solid black;
 width: 33%;
 text-align: center;
 border-collapse:collapse;
 background-color:lightblue;
}
table { margin: auto; }
</style>

</head>


<body>

<?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "weblab";
  $a=[];

  $conn = mysqli_connect($servername, $username, $password, $dbname);


if ($conn->connect_error)

die("Connection failed: " . $conn->connect_error);
$sql = "SELECT * FROM students";

$result = $conn->query($sql);

echo "<br>";
echo "<center> BEFORE SORTING </center>";
echo "<table border='2'>";
echo "<tr>";
echo "<th>USN</th><th>NAME</th><th>Address</th></tr>";
if ($result->num_rows> 0)
{
// output data of each row and fetches a result row as an associative array
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>". $row["usn"]."</td>";
echo "<td>". $row["name"]."</td>";
echo "<td>". $row["sem"]."</td></tr>";
array_push($a,$row["usn"]);
}
}
else
echo "Table is Empty";
echo "</table>";
$n=count($a);
$b=$a;
for ( $i = 0 ; $i< ($n - 1) ; $i++ )
{
$pos= $i;
for ( $j = $i + 1 ; $j < $n ; $j++ ) 
{
if ( $a[$pos] > $a[$j] )
$pos= $j;
}
if ( $pos!= $i )
{
$temp=$a[$i];
$a[$i] = $a[$pos];
$a[$pos] = $temp;
}
}
$c=[];
$d=[];
$result = $conn->query($sql);

if ($result->num_rows> 0)// output data of each row

{
while($row = $result->fetch_assoc()) 
{
for($i=0;$i<$n;$i++) 
{
if($row["usn"]== $a[$i])
{
$c[$i]=$row["name"];
$d[$i]=$row["sem"];
}
}
}
}

echo "<br>";

echo "<center> AFTER SORTING <center>";
echo "<table border='2'>";
echo "<tr>";
echo "<th>USN</th><th>NAME</th><th>Address</th></tr>";

for($i=0;$i<$n;$i++)

{
echo "<tr>";
echo "<td>". $a[$i]."</td>";
echo "<td>". $c[$i]."</td>";
echo "<td>". $d[$i]."</td></tr>";
}
echo "</table>";

$conn->close();

?>
</body>

</html>




***

Comments

Post a Comment

Popular posts from this blog

How to set image in carousel using flask?

Invalid syntax , perhaps you forgot a comma? Error in Python

How to run PL/SQL Code With Command Line?