Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1. Print current date
- echo date("F j, Y, g:i a");
- //2. Type of variable
- $boolVar = true;
- $intVar = 9;
- $doubVar = 1.23;
- $strVar = 'Coffee';
- $arrVar = array(3, 9, 4, 16);
- $nullVar = null;
- $resourceVar = mysql_connect();
- $objVar = (object) 'Ivan Ivanov';
- echo gettype($boolVar) . "</br>";
- echo gettype($intVar) . "</br>";
- echo gettype($doubVar) . "</br>";
- echo gettype($strVar) . "</br>";
- echo gettype($arrVar) . "</br>";
- echo gettype($nullVar) . "</br>";
- echo gettype($resourceVar) . "</br>";
- echo gettype($objVar);
- //3. Rectangle Area
- $a = 5;
- $b = 10;
- $area = $a * $b;
- echo "Rectangle's area is: " . $area;
- //4. Triangle Area
- $x1 = 15;
- $y1 = 1;
- $x2 = 25;
- $y2 = 52;
- $x3 = 32;
- $y3 = 13;
- $area = ($x1*($y2-$y3) + $x2*($y3-$y1) + $x3*($y1 - $y2))/2;
- echo "Triangles's area is: " . round(abs($area));
- //5. Boolean Variable
- //First version.
- $isFemale = true;
- $answer = ($isFemale) ? 'True' : 'False';
- echo "Am I a female? -" . $answer . "</br>";
- //Second version.
- if ($isFemale == true) {
- echo "I am a female.";
- }
- else
- echo "I am not a female.";
- //6. Quotes in Strings
- $withQuotes = "I asked a girl out and she said - \"I don't know\". Does she mean yes or no?";
- echo "Using quoted string: " . $withQuotes . "</br>";
- $withoutQuotes = <<<EOT
- I asked a girl out and she said - "I don't know". Does she mean yes or no?
- EOT;
- echo "Without using quoted string: " . $withoutQuotes;
- //7. Type Casting
- $float = 2.4;
- echo "$float is: " . gettype($float) . "</br>";
- settype($float, 'integer');
- echo "$float is: " . gettype($float);
- //8. Print Html
- $h = '<h1>Hello World</h1>';
- $p = '<p>I love Software Univerity</p>';
- echo htmlspecialchars($h) . "</br>";
- echo htmlspecialchars($p);
- //9. Divide by 2 and 9
- $int = 2;
- if ($int % 2 == 0) {
- if ($int % 9 == 0) {
- echo 'true';
- }
- else
- echo 'false';
- }
- else
- echo 'false';
- //10. Third Digit is 7?
- $int = 8143747;
- $result = (integer)$int / 100;
- if ($result % 10 == 7) {
- echo "true";
- }
- else
- echo "false";
- //11. Numbers from 1 to n
- $n = 20;
- for ($i = 1; $i <= $n; $i++) {
- echo $i . "</br>";
- }
- //12. Fibonacci numbers
- $x = 1;
- $y = 0;
- $n = 10;
- echo "0 ";
- for($i = 2; $i <= $n; $i++) {
- $z = $x + $y;
- echo $z . " ";
- $x = $y;
- $y = $z;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement