Hello im having an exam next week and this is what it will cover. all i ask is f
ID: 670292 • Letter: H
Question
Hello im having an exam next week and this is what it will cover. all i ask is for a few examples or explaining what it is and how to do it. thanks!
-Send PHP code to browser
-Comments
-Using variables (integer, Boolean and strings)
-Perform arithmetic
-Use constants
-Choosing method get or post
-Use simple IF statement
-Validating data
-Displaying and handle form in same script
-Sticky form
-Adding css, header and footer as “includes” (separate file)
-Be able to add simple PHP link to HTML code (PHP is separate file)
Explanation / Answer
(1) Send PHP code to browser :
Syntacs to display something in browser:
<?php echo "String to display";?>
example:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<?php echo "Welcome to PHP!";?>
</body>
</html>
(2) Using variables (integer, Boolean and strings):
-PHP variables starts with $ ,followed by variable name
-example:
<?php
$txt = "Hello world!"; // to store string value use quotes "" around value
$x = 5; //integer variable
$y = 10.5; //double variable
$z = True; // boolean variable
?>
(3) Perform arithmetic:
- Adding two numbers:
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
- Concat two string
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2; // output Hello World 1234
?>
(4) Use constants:
To define a constant use define() function and to retrieve the value of a constant, simply specify its name. Unlike with variables, you do not need to have a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically
Example:
<?php
define("MINSIZE", 50); //define a constatnt
echo MINSIZE; // will print value of const MINIMIZE,don't use $ before const
echo constant("MINSIZE"); // same thing as the previous line but using constant() function
?>
(5) Choosing method get or post:
Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.
$_GET is an array of variables passed to the current script via the URL parameters.
$_POST is an array of variables passed to the current script via the HTTP POST method.
When to use GET:
Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters
-"GET may be used for sending non-sensitive data"
When to use POST:
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
"POST may be used for sending sensitive data Like passwords"
(6) Use simple IF statement:
Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
example:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.