PHPx

nostalgicisolatedΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 1 μήνα)

51 εμφανίσεις

PHP

PHP Hypertext Preprocessor

Getting Started


PHP code starts with
<?
php

and ends with
?>
.



The file containing the PHP must have a file
extension of
.
php

(
not .html
)



PHP is a
server side
scripting language. PHP files
must reside on the server

in order for the PHP
script to be executed.


Variables


PHP is loosely typed.



All variables start with a $



The first character following the $ must be a letter
or underscore.



Variable can only contain letters, digits, underscores


Working with Strings


The concatenation operator is a dot (
.
), not a + sign.

JavaScript:
var

name = "Elizabeth " + "Hutchison";

PHP: $name = "Elizabeth " . "Hutchison";


JavaScript:
var

name = "Mickey";





name += "Mouse";


PHP


$name = "Mickey";





$name .= "Mouse";





String functions


The
strlen
() function is used to return the length of a
string.


strpos
() function
is used to search for character
within a string. Returns the starting position of the
string or false if string is not found.


$filename = "index.php";


strpos
($filename, "footer.php"); //returns false


strpos
($filename, "
php
"
); //returns 6

Note: This function
IS

case
-
sensitive!


Echoing Variables and Text Strings


Use echo to display


echo $filename;


echo "Hello world";



echo "Hello" . $filename;



echo
strpos
($filename, "footer.php");



echo
strpos
($filename, "
php
");



If statement

if (
condition
)
statement to be executed if true;


if (
condition
) {
statements to be executed if true;}


if (
condition
)


statement to be executed if true;

e
lse


statement to be executed if false;





$_SERVER


$_SERVER

is an array containing information such as
headers, paths, and script locations. The entries in
this array are created by the web server.




$_SERVER['HTTP_USER_AGENT'] info about user's
operating system, as well as their browser.



Try


echo $_SERVER['HTTP_USER_AGENT'] ;



$_SERVER['SERVER_ADDR] The IP address of the
server under which the current script is executing.



$_SERVER['SERVER_NAME'] The name of the server
host under which the current script is executing. If
the script is running on a virtual host, this will be the
value defined for that virtual host.



$_SERVER['SCRIPT_FILENAME'] returns the
pathname of the currently executing script.




Try


echo $_SERVER['SCRIPT_FILENAME'] ;



$_POST


$_POST

is used
to collect values in a form with
method="post
".


The $_POST array is an associative

array

of variables
passed to the current script via the HTTP POST method
.


the
names (not ids)

of the form fields will
automatically be the keys in the $
_POST
array


Examples:


$pay= $_
POST
[
"
wage"];


$greeting =
"
Hello “ . $_
POST
[“name"];



<
?
php

echo $_POST["major"];
?>




include


In PHP, you can insert the content of one PHP file into
another PHP file
before

it is served.


The html
segment
should be valid html. It is not an
entire html file, just the segment of html that you want
inserted.


Syntax: include ('
filename
‘);


Example
: <
?
php


include (‘
menu.php
‘);
?
>




Java Script tips


Shorthand


This:


var

$ = function(id) { return
document.getElementById
(id);
}


$("error_message1").
innerHTML

= "
Oops!";


$("
error_message2").
innerHTML

= "
Oops again!";


$("error_message3").
innerHTML

=

TripleOops
!";



Is the same as this


document.getElementById
("
error_message
").
innerHTML

= "Oops
!";


document.getElementById
("
error_message2").
innerHTML

= "Oops again!";


document.getElementById
("
error_message3").
innerHTML

= “
TripleOops
!";



Java Script tips


Converting strings to numbers


parseInt
(x):


Parses a string read from a form to an integer


var

hours =
parseInt
($(“hours").
value
);



parseFloat
(x)


Parses a string read from a form to an
floating point number


var

wage=
parseFloat
($(“wage").
value
);



isNaN
(x)


Returns true of x is Not a Number, false if it is a number


isNaN
(wage) would return true if the user left the wage entry blank or entered text.


Java Script tips


Spans rock!


Create a span in your html that you can later add text to when an
error occurs.



$(“
wage_error
").
innerHTML

=
"*Wage must be greater than 7.25";



Don’t forget you can change the style (
css
) using script as well.


$(“
wage_error
").
style.color

=
“red";


$(“
wage_error
").
style.backgroundColor

=
“green";


$(“
wage_error
").
style.fontWeight

= "bolder";



$(“
wage_error
").
style.fontSize

= "200
%";


Etc…..

Disabling buttons


Html


Use disabled Attribute to keep a user from clicking a button


disabled="disabled"



JavaScript



$("submit").disabled=false
;



$("submit").
disabled=true;

Lab Time!