Вы находитесь на странице: 1из 5

c:\wamp\logs - for error logs in php development.

<?php ?>
echo similar to print
print prints to screen
"Hello" . " World!" String concatination with a period
$ Use at the start of a variable (variables are case
sensitive with no spaces)
echo "<br />"; creates a break in the lines.
echo "Hello World<br />"; Strings can contain html line breaks and can be done
in single quotes.
echo "{$variable} Again"; This also concatinates but can only be done with
double quotes.
echo $variable."Again";
<br /> This line break can be used outside of php in the
html side.
Name: Returns the name and output when echo is used.

Function to strings

.= Appends string and string Variables


strtolower(variable) Converts all strings to uppercase (can be variable or
string in quotes)
strtoupper(variable) Converts all strings to lowercase (can be variable or
string in quotes)
ucfirst(variable) Converts first word in sentence to uppercase (can be
variable or string in quotes)
ucwords(variable) Converts the start of every word to uppercase (can be
variable or string in quotes)

strlen(variable) Gets the length of the string


trim(variable) Eliminates white spaces between words
strstr(varable, "word") Searches for a word in a string
str_replace("replace word", "with this word", variable) replaces one word with
another in a sting or variable of strings.
str_repeat(variable,2) Repeats the string a number of times, in this case
two.
substr(variable,5,10) Makes a substring by taking a starting and ending
position.
strpos(variable, "word") Gives the position of a word with in a string
sentence.
strchr(variable, "z") Finds a character within a string.

Numbers

variable += 4 Method that updates or adds 4(any number) to whatever


was already in the variable.
variable -= 4
variable *= 4
variable /= 4

variable++ Increments variable by one


variable-- Decrements variable by one

round(variable, 2) Rounds to two decimal places


ceil(variable) Rounds to the next integer number
floor(variable) Rounds to the lower integer number
abs(0 - 300) Gives the positive or magnitude of a number
pow(x, y) Gives x to the power of y
sprt(number) Returns the square root of a number
fmod(x, y) Modulus of x/y (remainder)
rand() Returns a random number
rand(x, y) Randum between x and y.

$array1 = array(4,8,15,16,23,42); Creates an array of numbers


$array1[1]; Calls the second element in an array since arrays
start at position zero.

array("first_name" => "Kevin"); Associative array, can call first_name


instead of Kevin
print_r($array); Returns all the content of an array

count(array); Counts the elements in an array


max(array); Returns the max value in an array
min(array); Returns the min value in an array
sort(array); Sorts the values in an array from lowest to
highest
rsort($array1); Sorts the values in an array from highest to
lowest

echo $string1 = implode(" * ", $array1); Takes the values from an array and
creates a string sperated by asterisks
print_r (explode(" * ", $string1)); Takes a string and seperates the elements
at the asterisks and creates an array.
in_arry(element, array) Searches for an element in an array and
returns nothing or a 1 for F/T

Boolean

isset(variable); Asks whether a variable is set and returns a boolean


expression (0, "0", and NULL are all considered empty)
unset(variable); Makes a variable Empty or Null
empty(variable); Asks whether a variable is empty and returns a
boolean expression (0, "0", and NULL are all considered empty)
>= Greater or equal to
<= Less than or equal to
== Equal to
!= Not equal to
&& Logical AND statements, can also use AND.
|| Logical OR statement

Typecasting

settype(variable, "string"); Sets the variable to integer, string or any


other type
gettype(variable); Asks what type the variable is.
is_array($val1); Asks whether the variable is an array
is_bool($val1); Asks whether the variable is an boolean
is_float($val1); Asks whether the variable is a float
is_int($val1); Asks whether the variable is an integer
is_null($val1); Asks whether the variable is null
is_numeric($val1); Asks whether the variable is numeric
is_string($val1); Asks whether the variable is a string
Constants

define("name", constant); Defines a word as a constant. As opposed to a


variable, a constant cannot be changed.

IF statements
if(expression) {statements;} If statements evaluates as a boolean
expression
elseif(expression) {statements;} elseif
else{statements;} else

Switch statement

switch ($a){
case 0:
echo "a equals 0"
break;
case 1:
echo "a equals 1";
break;
case 2:
echo "a equals 2";
break;
default:
echo "a is not 0, 1 or 2";
break;}

Loop Statements

while(expression) {statements;}
$count ++;

For Loop

for(expression1, expression2, expression3) for(initial,


test, each)
{statements;}

foreach ($array as $value)


{statements;}

foreach ($array as $key => $value)


{statements;}

continue; Jump to the very top of the loop without executing


any other command
break; Breaks out of the loop at that point.

Pointers

current($array) Returns the current element of an array


starting at the first element
next($array) Goes to the next element in an array

Function

function name(arguments){statements;}
Return Values

return variable; Returns a value outside of a function. Only one


variable is returned otherwise an array must be created.
global variable; Makes a variable global where one was local before
(within a function). Use right after the function declare.

Debugging functions

echo $variable;
print_r($array)
gettype($variable);
var_dump($variable); Gets variable type and value.
get_defined_vars(); Returns an array of all defined variables.

Creating websites

GET for URL/Links (The only one that need encoding).


POST Used to create forms to fill out. This is also
created as an array.
COOKIE A value that has already been installed on a
users browser.

<a href="secondpage.php">Second Page</a> Creates a link called Second page


which opens secondpage.php
<a href="secondpage.php?id=1">Second Page</a> Sends values along with the href
(id=1 in this example). Can use & to pass other data

<?php Code must be in the second page to "grab" the


id value from firstpage.
$id = $_GET['id'];
echo "<br />" $id
?>

href="secondpage.php?name=<?php echo urlencode("Me&"); ?>&id=1"> urlencode is


used to pass values such as &. Needs to be within php tags.

<?php Code must be in the second page to "grab" the


id value from firstpage.
$id = $_GET['id'];
$name = $_GET['name'];
echo "<br />" $id ": {$name}";
?>

rawurlencode Uses normal url tags used by server of


the php one such as %20 instead of + for spaces.
Use for dynamic links found before the question
mark.

$linktext = "<Click> & you'll see";


href="secondpage.php?id=1"<?php echo htmlspecialchars($linktext); ?>
htmlspecialchars used to excape all special characters used in a link name.

Global Variables

_GET Creates an associative array

_POST
Used along with forms. This is also in the
form of an associative array.
$username = $_POST['username'];

echo "{$username}: {$password}";

setcookie($name, $value, $expire); _COOKIE['test']; Also


returns an associative array. Value of 0 is used when expired.

ex. <?php setcookie('test', 45, time()+(60*60*24*7)); ?> Where time


is given in seconds.

Session is file stored on webserver. Can store more info but lasts shorter than
cookie. Needs to be used as a header. Keeps info on server and not on user name.

<?php session_start(); ?> <?php $_SESSION['name] = "name;"?>

When using header information in the body. Headers are usually before
html tags, but can use this code to initiate the output buffer

<?php header("HTTP/1.0 404 Not Found"); exit;?>


<?php header("Location: basic.html"); exit;?> This is a
302 link redirect. Redirect entire page without doing anything else.

The following starts the output buffer to include headers and cookies in the body
of the html.

ob_start();
<html>

</html>
ob_end_flush();

Include and Require To be used with functions.

include("file to be included.php"); Could be php code, text or other


forms of information. Does not need include to call functions.
require(); Same as include but will generate an
error and does not continue with code.
require_once(); Files that should not be included
more than once.

if (!$connection){
die("Message: " . mysql_error()); To avoid returning errors
when using require_once.

Вам также может понравиться