Posts

Showing posts from December, 2021

How to create age calculator program in php

  Agecalculator program In the below example Create a HTML script to take input from users separated by "/" current(year, month, date) and his date of birth (year, month, date). Convert the inputted value mm/dd/yyyy string into array using explode( ) function for both current date and date of birth. strore these values in $arr variable at 0 index date, at 1 index month and at 2 index year like $arr[0]=date $arr[1]=month $arr[2]=year same as for (date of birth) inside $brr variable. Now check for  date  if current "date" is less than date of birth "date" then borrow 1 month and add 30days in current date and calculate. Same as for  Month  here borrow 1year from existing year and add 12 months. subtract current months with date of birth "month" and store in $mon variable. at last calculate for  year  and store the value in $yr variable. Display the all calculated values(Year,Month, Date) like: 26 years 4 months 0 days <?php error_reporting(1); $...

How to write simple calculator program in php with html

calculator.php  <?php  extract($_POST); if(isset($save)) { switch($ch) { case '+': $res=$fn+$sn; break; case '-': $res=$fn-$sn; break; case '*': $res=$fn*$sn; break; case '/': $res=$fn/$sn; break; } } ?> <!DOCTYP html> <html> <head> <title>Calculator- switch case</title> </head> <body> <form method="post"> <table border="1" align="center"> <tr> <th>Your Result</th> <th><input type="number" readonly="readonly" disabled="disabled" value="<?php  echo @$res;?>"/></th> </tr>  <tr> <th>Enter your First num</th> <th><input type="number" name="fn" value="<?php  echo @$fn;?>"/></th> </tr>  <tr> ...

How to create the Session and count the session program in php

What is a Session Although you can store data using cookies but it has some security issues. Since cookies are stored on user's computer it is possible for an attacker to easily modify a cookie content to insert potentially harmful data in your application that might break your application. Also every time the browser requests a URL to the server, all the cookie data for a website is automatically sent to the server within the request. It means if you have stored 5 cookies on user's system, each having 4KB in size, the browser needs to upload 20KB of data each time the user views a page, which can affect your site's performance. You can solve both of these issues by using the PHP session. A PHP session stores data on the server rather than user's computer. In a session based environment, every user is identified through a unique number called session identifier or SID. This unique session ID is used to link each user with their own information on the server like emails,...

How to set cookies and display the Last visited date and time on the web page in php program

  Cookies are text files stored on the client computer and they are kept of use tracking purpose. PHP transparently supports HTTP cookies.   Setting Cookies with PHP PHP provided  setcookie()  function to set a cookie. This function requires upto six arguments and should be called before <html> tag. For each cookie this function has to be called separately. setcookie(name, value, expire, path, domain, security); Here is the detail of all the arguments − ·         Name  − This sets the name of the cookie and is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used while accessing cookies. ·         Value  − This sets the value of the named variable and is the content that you actually want to store. ·         Expiry  − This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970. After this...

How to generate the multiplication table in php

Design a Simple web page to generate multiplication table for a given number <?php  $number=empty($_POST["number"])?1:$_POST["number"];   ?>  <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Write a PHP Program to Print Multiplication Table of a Number </title> </head> <body>     <form action="" method="post">     <input type="text" name="number" value="<?=$number?>" placeholder="ex:3">     <button type="submit">Calculate</button>     </form>     <?php echo $number ; ?>          <table border="1" width="200">     <?php  for($i=0;$i<=20;$i++){ ?>         <tr>             <td>...

How to run GCD program in php

    GCD  stands for  Greatest Common Divisor . The GCD of two numbers is the largest number that divides both of them. For example - GCD of 20 and 25 is 5, and GCD of 50 and 100 is 50. Method 1: Using For Loop to find GCD of two numbers In the example below, for loop is used to iterate the variable  i  from 0 to the smaller number. If both numbers are divisible by  i , then it modifies the GCD and finally gives the GCD of two numbers.   <?php $x = 50; $y = 100;   if ($x > $y) {   $temp = $x;   $x = $y;   $y = $temp; }   for($i = 1; $i < ($x+1); $i++) {   if ($x%$i == 0 and $y%$i == 0)     $gcd = $i; }   echo "GCD of $x and $y is: $gcd"; ?> The above code will give the following output: GCD of 50 and 100 is: 50 Method 2: Using While Loop to find GCD of two numbers In the example below, larger number is replaced by a number which is ...

Factorial Program in PHP

Factorial of a number is the product of all numbers starting from 1 up to the number itself. While calculating the product of all the numbers, the number is itself included. Factorial of a number is calculated for positive integers only. The factorial of 0 is always 1 and the factorial of a negative number does not exist. It is denoted by ‘!’ preceded by the number. Example n! where n is the number So, Factorial of 5! means factorial of 5 Factorial of 7! means factorial of 7 For example, the factorial of number 5 is: 5! =5*4*3*2*1 = 120 Similarly, the factorial of number 7 is: 7! = 7*6*5*4*3*2*1 = 5040 and so on.. Now how do we actually find the factorial, we can do it using for loop (without recursion) with recursion Factorial Logic The logic behind getting the factorial of the number is as per the following. Get the number whose factorial is to be calculated. Get all the numbers starting from 1 up to that number. Get the...