Friday, November 19, 2010

[PHP] Cheat sheet, part 1 - basics

I know there are dozens of similar stuff on net, but none would be as good for me as one made by myself. Created while reading (mainly) a brick called PHP and MySQL Web Development: Vademecum.

Putting PHP into HTML
<?php
echo "<p>Text bla bla bla</p>";
?>

<?php echo '<p>Text bla bla bla</p>'; ?>


Comments
/*
long
comments
*/


echo '<p>Text bla bla bla.'; // short comment
echo '<p>Text bla bla bla.'; # short comment


. – bonds 2 strings

Date
<?php
echo '<p>' . date('H:i. jS F Y') . '</p>' ;
?>

result: 14:32. 19th June 2010

date formatting
H – 24-hour format of an hour with leading zeros
i – minutes
j – day (without zero)
S – +th
F – month (verbal)
Y – year
more here

variables – access, displaying, types

access to the content of $tyresamount
$_POST[‘tyresamount’]

displaying
echo $tyresamount.' tyres<br /> ' ;
echo "$tyresamount tyres< br />" ;

interpolation – inside a string, replacing a variable by its content, works with "

types
• Integer - integers; -2, 3
• Float (called also Double) - real number; 2.4, 0.3
• String - text; boobphysicist
• Boolean - 0 or 1
• Array - used to store many variables
• Object - used to store objects

logical operators
$a and $b
TRUE if both $a and $b are TRUE.
$a or $b
TRUE if either $a or $b is TRUE.
$a xor $b
TRUE if either $a or $b is TRUE, but not both.
! $a
TRUE if $a is not TRUE.
$a && $b
TRUE if both $a and $b are TRUE.
$a || $b
TRUE if either $a or $b is TRUE.















formatting variables
$amount = 0;
$value = (float)$amount;

formats 0 to real number

assignment operators – shortened operations
$a  +=  $b      
$a  = $a  + $b  
$a  -= $b 
$a  = $a  - $b
$a  *=  $b
$a  = $a  *  $b
$a  /= $b 
$a  = $a /  $b
$a %=  $b 
$a  = $a %  $b
$a  .= $b  
$a  = $a  $b


reference operator – „combining” variables
$a = 5;
$b = &$a;
$a = 7; // both $a and $b has value 7

unset($a) ; - undos it

comparison operators
== - is equal? (= is an assignment operator!)
=== - is identical?
!= - isn’t equal?
!== - isn’t identical?
<> - inequality
< >
<= >=


dividing 1 variable into 2
( $mark > 2 ? . ‘positive' : 'negative' );

Divides students’ marks into ‘positive’ and ‘negative’.

ignoring errors
$a = @(57/0);

listing folders
<?php
$out = `dir c:` ;
echo "<pre>".$out."</pre>";
?>


Checking and setting the type of a variable
$a = 56;
echo gettype($a).’’; // tells that $a is an integer number
settype($a, ‘double’); // turns it into real number, $a = 56.0
echo gettype($a).’’; // tells that $a is a real number


checks if the variable is a...
is_array()
is_double(), is_float(), is_real() – real number
is_long(), is_int(), is_integer – integer number
is_string()
is_bool()
is_object()
is_resource()
is_null()
is_scalar()

is_numeric() – has a numeric value
is_callable() – is a name of an existing function

useful while checking if the user filled specific places correctly
echo 'isset( $ema):'. isset($ema). '': // returns true if the variable exists
echo 'empty($tyresamount):'. empty($tyresamount). '': // true, if doesn’t exist or is empty


Control structures
if($amount==0)
echo 'No order placed on the previous site!' ;


To be continued.
btw. haven't I mentioned before that blogger's text formatting sucks badly?
price for being too lazy to build an own site, I guess >.>

2 comments:

  1. WOah. Ostatnim razem byłem tu chyba z 2 miesiące temu xD. To samo co napisałeś jest na www.php.net oraz www.w3schools.com (PHP Reference sobie już znajdziesz sam) tylko w bardziej przystępniejszej formie. xD

    Jak będziesz miał czas to proponuję zapoznać się z jakimś frameworkiem dla php, np. PHP Symphony. Niedługo będę testował (czytaj. muszę zrobić taki projekt) więc podzielę się spostrzeżeniami.

    Tak btw. robisz jakiś konkretny projekt czy tak na razie tylko dla zabawy?

    ReplyDelete
  2. "To samo co napisałeś jest na www.php.net oraz www.w3schools.com (PHP Reference sobie już znajdziesz sam) tylko w bardziej przystępniejszej formie. xD"
    Też z tego czasem korzystam, ale pisałem w 1szym zdaniu że wolę zrobić coś podobnego samemu, dla siebie. Z tym, że dla wygody przerzucam się na wersję drukowaną, więc na blogu części dalszej (bynajmniej w postaci posta) nie będzie. Za dużo pieprzenia z formatowaniem tekstu - GUI postowania na bloggerze nie działa do końca tak jak powinien, a na ręczne formatowanie schodzi za dużo czasu. Szkoda tylko, że na kartce papieru nie działa crtl+F xd coś za coś. Czas pokaże co będzie wygodniejsze.

    Ok, dodałem do Bookmarksów ;>

    Tak się tylko bawię, równolegle z C# który mam na uczelni. Na projekt przyjdzie czas, teraz zaliczenie maty jest priorytetem x.x

    ReplyDelete