Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Print current date
- <!--Try to find date time function and print with echo or print functions current date.-->
- <?php
- echo "Today is " . date("Y/m/d") . "<br>";
- echo "Today is " . date("Y.m.d") . "<br>";
- echo "Today is " . date("d-m-Y") . "<br>";
- echo "Today is " . date("l");
- ?>
- 2. Type of variable
- <!--Declare all possible types of variable and then, print their types with echo. You can use function gettype() that
- return type of given variable.-->
- <?php
- $data = array(1, 1., NULL, new stdClass, 'foo');
- foreach ($data as $value)
- {
- echo gettype($value), "\n";
- }
- ?>
- 3. Rectangle Area
- <!--Write a program that enters the sides of a rectangle (two integers a and b) and calculates and prints the rectangle's
- area. Examples:
- Input Output
- 7 20 140
- 5 12 60-->
- <form action="3_ RectangleArea.php" method="post">
- <input type="text" name="width" value="Width">
- <input type="text" name="height" value="Height">
- <select name="select">
- <option value="rectangle">Rectangle</option>
- <option value="triangle">Triangle</option>
- </select>
- <input type="submit" value="Go">
- </form>
- <?php
- if($_POST['select'] === 'rectangle')
- {
- echo 'Result: '. $_POST['width'] * $_POST['height'];
- }
- elseif($_POST['select'] === 'triangle')
- {
- echo 'Result: '. $_POST['width'] * $_POST['height']/2;
- }
- ?>
- 4. Triangle Area
- <!--Write a program that enters 3 points in the plane (as integer x and y coordinates), calculates and prints the area of
- the triangle composed by these 3 points. Round the result to a whole number. In case the three points do not form
- a triangle, print "0" as result. Examples:
- Input Output Input Output Input Output
- -5 10 575
- 25 30
- 60 15
- 53 18 86
- 56 23
- 24 27
- 1 1 0
- 2 2
- 3 3-->
- <h1 style="font-size: 1.2em">Area of a Triangle</h1>
- <form action="4_TriangleArea.php" method="post">
- <input type="text" name="x1" value="Width(x1)"><br>
- <input type="text" name="y1" value="Height(y1)"><br>
- <input type="text" name="x2" value="Width(x2)"><br>
- <input type="text" name="y2" value="Height(y2)"><br>
- <input type="text" name="x3" value="Width(x3)"><br>
- <input type="text" name="y3" value="Height(y3)">
- <input type="submit" value="Go">
- </form>
- <?php
- $x1 = $_POST['x1'];
- $y1 = $_POST['y1'];
- $x2 = $_POST['x2'];
- $y2 = $_POST['y2'];
- $x3 = $_POST['x3'];
- $y3 = $_POST['y3'];
- $aBool = (($x1*($y2-$y3))+($x2*($y3-$y1))+($x3*($y1-$y2)))/2;
- echo 'Area:'.$aBool;
- ?>
- 5. Boolean Variable
- <!--Declare a Boolean variable called isFemale and assign an appropriate value corresponding to your gender. Print it
- on the web page.-->
- <form action="5_BooleanVariable.php" method="post">
- <div>My gender is? </div>
- <select name="select">
- <option></option>
- <option value="male">Male</option>
- <option value="female">Female</option>
- </select>
- <input type="submit" value="Submit">
- </form>
- <?php
- $select = $_POST['select'];
- $myGender = TRUE;
- if($select === 'male')
- {
- $myGender = TRUE;
- echo 'Male ';
- echo $myGender;
- }
- elseif($select === 'female')
- {
- $myGender = FALSE;
- echo 'Female';
- echo $myGender;
- }
- ?>
- 6. Quotes in Strings
- <!--Declare two string variables and assign them with following value:
- I asked a girl out and she said – “I don't know”. Does she mean yes or no?
- Do the above in two different ways: with and without using quoted strings. Print the variables to ensure that their
- value was correctly defined.-->
- <?php
- $a = 'I asked a girl out and she said - ';
- $b = "\"I don't know\". ";
- $c = 'Does she mean yes or no?';
- echo 'I asked a girl out and she said - '. $b . 'Does she mean yes or no?'.'<br>';
- echo $a,$b,$c;
- ?>
- 7. Type Casting
- <!--You can pretend that a variable or value is of a different type by using a type cast. Try to cast floating point number
- to integer and then print it on the web page.-->
- <?php
- $a = 3.2;
- $b = 3;
- echo (int)$a;
- 8. Print Html
- ?
- ?>
- 9. *Divide by 2 and 9
- <!--Write a Boolean expression that checks for given integer if it can be divided (without remainder) by 7 and 5 in the
- same time. Examples:
- n Divided by 7 and 5?
- 17 false
- 0 false
- 10 false
- 7 false
- 18 true
- 72 true-->
- <form action="9_DivideBy_2_ and_ 9.php" method="post">
- <input type="text" name="n" value="integer">
- <input type="submit" value="Go">
- <input type="reset" value="Reset">
- </form>
- <?php
- $aBool = TRUE;
- $n = $_POST['n'];
- if($n >= 1)
- {
- if($n % 2 == 0 || $n % 9 == 0)
- {
- $aBool = TRUE;
- echo 'True';
- }
- else
- {
- $aBool = FALSE;
- echo 'False';
- }
- }
- else
- {
- $aBool = FALSE;
- echo 'False';
- }
- ?>
- 10. *Third Digit is 7?
- <!--Write an expression that checks for given integer if its third digit from right-to-left is 7. Examples:
- n Third digit 7?
- 5 false
- 701 true
- 9703 true
- 877 false
- 777877 false
- 9999799 true-->
- <div>Write integer:</div>
- <form action="10_ThirdDigitIis_7.php" method="post">
- <input type="text" name="int">
- <input type="submit" value="Push">
- </form>
- <?php
- $n = $_POST['int'];
- $aBool =($n/100)%10==7;
- if($aBool == 7){
- echo 'True';
- }
- else {
- echo 'False';
- }
- ?>
- 11. **Numbers from 1 to n
- <!--Write a program that reads an integer number n from the input and prints all the numbers in the interval [1..n],
- each on a single line. Note that you may need to use a for-loop. Examples:
- numbers sum numbers sum numbers sum-->
- <div>Write integer:</div>
- <form action="11_NumbersFrom1_To_ n.php" method="post">
- <input type="text" name="int">
- <input type="submit" value="Push">
- </form>
- <?php
- $n = $_POST['int'];
- for($i=0;$i<=$n;$i++){
- echo $i.'</br>';
- $sum+=$i;
- }
- echo 'Sum: '.$sum.'</br>';
- ?>
- Problem 12. ***Fibonacci Numbers
- <!--Write a program that reads a number n and prints on the web page the first n members of the Fibonacci sequence
- (at a single line, separated by spaces) : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, .... Note that you may need to
- learn how to use loops. Examples:
- n comments
- 1 0
- 3 0 1 1
- 10 0 1 1 2 3 5 8 13 21 34-->
- <div>Write integer:</div>
- <form action="12_FibonacciNumbers.php" method="post">
- <input type="text" name="int">
- <input type="submit" value="Push">
- </form>
- <?php
- $n = $_POST['int'];
- $a = 1;
- $b = 1;
- $c = 0;
- $counter = 0;
- for($i=0;$i<$n;$i++){
- $counter++;
- $c = $a+$b;
- $a = $b;
- $b = $c;
- $c = $a+$b;
- echo $c.'</br>';
- }
- echo $n.' Item';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment