Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
57 Cards in this Set
- Front
- Back
SYNTAX:
basic variable |
$var_name = value;
NOTE: ***must have $ sign in front. ***line must end with semi-colon; ***variable does not need to be declared before using it. |
|
NAMING RULES for variables:
|
* A variable name must start with a letter or an underscore "_"
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) * A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString) |
|
There is only one "string operator" in PHP
|
(.) use the period put strings values together
EXAMPLE: <?php $txt1="Hello World!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2; ?> |
|
SYNTAX:
return the length of a string. |
the strlen() function
EXAMPLE: <?php echo strlen("Hello world!"); ?> |
|
SYNTAX:
find the position of a match |
the strpos() function
EXAMPLE: <?php echo strpos("Hello world!","world"); ?> |
|
SYNTAX:
The if...else statement |
if (condition)
code to be executed if condition is true; else code to be executed if condition is false; EXAMPLE: <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html> |
|
SYNTAX:
The if...elseif...else statement |
if (condition)
code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false; |
|
SYNTAX:
The switch statement |
switch (n)
{ case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; } |
|
SYNTAX:
numeric array |
$cars=array("Saab","Volvo","BMW","Toyota");
OR $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; |
|
EXAMPLE:
calling a "numeric array" |
<?php
$cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars."; ?> |
|
What is a "numeric array"?
|
A numeric array stores each array element with a numeric index.
|
|
What is an "associative array"?
|
*** An associative array, each ID key is associated with a value.
*** With associative arrays we can use the values as keys and assign values to them. |
|
EXAMPLE:
associative array |
<?php
$ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?> |
|
Definition:
What is a multidimensional array. |
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.
|
|
EXAMPLE:
create a multidimensional array, with automatically assigned ID keys: |
$families = array
( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); |
|
SYNTAX:
a while loop |
while (condition)
{ code to be executed; } |
|
EXAMPLE:
while loop |
<html>
<body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html> |
|
SYNTAX:
do...while |
do
{ code to be executed; } while (condition); NOTE: the do..while will execute at least once. |
|
SYNTAX:
for loop |
for (init; condition; increment)
{ code to be executed; } |
|
SYNTAX:
foreach loop |
foreach ($array as $value)
{ code to be executed; } |
|
EXAMPLE:
foreach loop |
<html>
<body> <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value . "<br />"; } ?> </body> </html> |
|
SYNTAX:
create a PHP function |
function functionName()
{ code to be executed; } PHP function guidelines: *** Give the function a name that reflects what the function does *** The function name can start with a letter or underscore (not a number) |
|
FUNCTION:
WHICH function takes all the content in a specified file and includes it in the current file? |
include() function
....takes all the content in a specified file and includes it in the current file. NOTE: If an error occurs, the include() function generates a warning, but the script will continue execution. EXAMPLE: <html> <body> <?php include("header.php"); ?> <h1>Welcome to my home page!</h1> <p>Some text.</p> </body> </html> |
|
FUNCTION:
WHICH function is similar to include() ? AND what is the difference |
require()
DIFFERENCE: the require() function generates a "fatal error" and the script will stop. |
|
FUNCTION:
HOW do you open a file in PHP? |
fopen()
EXAMPLE: <html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html> |
|
List some "modes" of
fopen() |
Modes Description
r Read only. Starts at the beginning of the file r+ Read/Write. Starts at the beginning of the file w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist a+ Read/Append. Preserves file content by writing to the end of the file x Write only. Creates a new file. Returns FALSE and an error if file already exists x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists |
|
give an example of an fopen message...if it is unable to open a file.
|
EXAMPLE:
<html> <body> <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?> </body> </html> |
|
FUNCTION:
How to close an opened file. |
fclose($file);
EXAMPLE: <?php $file = fopen("test.txt","r"); //some code to be executed fclose($file); ?> |
|
FUNCTION:
How to check the end-of-file |
feof($file)
NOTE: You cannot read from files opened in w, a, and x mode! EXAMPLE: if (feof($file)) echo "End of file"; |
|
FUNCTION:
how to read a file line-by-line. |
fgets()
...is used to read a single line from a file. EXAMPLE: <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?> |
|
FUNCTION:
how to read a file character-by-character. |
fgetc()
... is used to read a single character from a file. EXAMPLE: <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?> |
|
SYNTAX:
create a "constant" |
define("constantname","constantvalue");
NOTE: constants are like variables, except that they are not changed. |
|
SYNTAX:
calling a "constant" |
EXAMPLE:
echo contstantName; NOTE: *** constants do not use $ sign. *** constants cannot use quotes. |
|
PHP predefined variables (superglobals):
...that contains any variables provided to a script through the GET method. |
$_GET
|
|
PHP predefined variables (superglobals):
...that contains any variables provided to a script through the POST method. |
$_POST
|
|
PHP predefined variables (superglobals):
...that contains any variables provided to a script through a cookie. |
$_COOKIE
|
|
PHP predefined variables (superglobals):
...that contains any variables provided to a script through file uploads. |
$_FILES
|
|
PHP predefined variables (superglobals):
...that contains information such as headers, file paths, and script locations. |
$_SERVER
|
|
PHP predefined variables (superglobals):
...that any variables provided to a script as part of the server environment. |
$_ENV
|
|
PHP predefined variables (superglobals):
...that contains any variables provided to a script via GET, POST, or COOKIE input mechanisms. |
$_REQUEST
|
|
PHP predefined variables (superglobals):
...that contains any variables that are currently registered in a session. |
$_SESSION
|
|
PHP Variable Testing (data types):
checks if variable is null |
.is_null($variable);
|
|
PHP Variable Testing (data types):
checks if variable is a integer |
.is_int($variable);
|
|
PHP Variable Testing (data types):
checks if variable is a string |
.is_string($variable);
|
|
PHP Variable Testing (data types):
checks if variable is a floating point (or double) |
.is_double($variable);
|
|
PHP Variable Testing (data types):
checks if variable is boolean |
.is_bool($variable);
|
|
PHP Variable Testing (data types):
checks if variable is an array |
.is_array($variable);
|
|
PHP Variable Testing (data types):
checks if variable is a number |
.is_numeric($variable);
|
|
PHP Variable Testing (data types):
checks if variable is a resource |
.is_resource($variable);
|
|
PHP Variable Testing (data types):
test if variable is null |
.is_array($testing);
|
|
PHP VARIABLES:
How to set the type of variable. |
settype($variabletochange, 'new type');
|
|
PHP VARIABLES:
Changing Type by Casting. What is this? |
EXAMPLE:
$newvar = (integer) $originalvar EXPLANATION: creates a copy of original (does not affect the original) |
|
PHP "predefined constants":
...what will return the name of the file that the PHP engine is currently reading. |
__FILE__
|
|
PHP "predefined constants":
...that will return the current line number of the file. |
__LINE__
|
|
PHP "predefined constants":
...what will give the version of PHP is interpreting the script |
PHP_VERSION
|
|
Four Methods:
How to make system calls using PHP |
1. backticks ` `
2. system("command; command "); 3. exec() 4. pasthru() |
|
what line can be added at the top of a php script to display errors
|
init_set("display_errors", "On");
WARNING: script debugging only. NOT good for public facing pages |