InfiniteSkills Learning PHP MySQL
InfiniteSkills Learning PHP MySQL
KONSTATNTY
// int
define('LEVEL_1', 1);
// float
define('PI', 22/7);
// string
define('FILE_ERROR', 'ERROR: unable to open a file!');
// boolean
define('VALID', TRUE);
// constants can be assigned to variables
$pi = PI;
// constants can be echoed
echo FILE_ERROR;
// current filename
echo __FILE__;
//current dir
echo __DIR__;
<?php
$name = 'Alexandria';
$accountNumber = 1234;
$balance = 1443.22;
?>
<table border="1" cellpadding="5">
<tr>
<th>Name</th>
<th>Account Number</th>
<th>Balance</th>
</tr>
<tr>
<td><pre><?php echo $name ?></pre></td>
<td><pre><?php echo $accountNumber ?></pre></td>
<td><pre><?php echo $balance ?></pre></td>
</tr>
</table>
$name = 'Alexandria';
$accountNumber = 1234;
$balance = 1443.22;
$cislo = 9876;
echo '<table border="1" cellpadding="5">';
echo '<tr><th>Name</th><th>Account
Number</th><th>Balance</th><th>slo</tr></tr>';
echo '<tr>';
// %s = string %d = digits %.2f = floating point with 2 decimal places
// try adding a number after the "%" sign
printf('<td><pre>%20s</pre></td><td><pre>%12d</pre></td><td><pre> %.2f</pre></td><pre><td>%08d</td></pre>',
$name, $accountNumber, $balance, $cislo);
echo '</tr>';
echo '</table>';
<pre></pre> Vypisuje formtovan text
SUBSTRING
// syntax substr($string, $start, $length)
$firstName = 'Doug';
$lastName = 'Bierer';
printf('<br />Full Name: %s %s', $firstName, $lastName);
// use substr to get the 1st initials
// NOTE: strings start with 0, not 1!
printf('<br />Initials: %s%s', substr($firstName, 2, 2), substr($lastName, 0, 3));
$filename = __FILE__;
$extension = substr($filename, -8);
printf('<br />File Name: %s', $filename);
printf('<br />Extension: %s', $extension);
Prce s etzci
$street
= 'Main Street';
$number = 248;
$city
= 'Toronto';
$province = 'Ontario';
$address = $number . ' ' . $street . ', ' . $city . ', ' . $province;
echo "<br />Full Address Using '.' :<br /> $address";
// sprintf vrac hodnotu
$address = sprintf('%d %s, %s, %s', $number, $street, $city, $province);
echo "<br />Full Address Using sprintf :<br /> $address";
Oznut etzce
// assign values
$untrimmedString = ' This is a string with spaces on the ends. ';
// use strlen() to get the length of the untrimmed string
$lengthUntrimmed = strlen($untrimmedString);
// now apply trim()
$trimmedString = trim($untrimmedString);
// get the length of the trimmed string
$lengthTrimmed = strlen($trimmedString);
// display the results:
printf('<br />String Before -->|%s|<--', $untrimmedString);
printf('<br />String After ---->|%s|<--', $trimmedString);
printf('<br />Length Before --> %d', $lengthUntrimmed);
printf('<br />Length After ---> %d', $lengthTrimmed);
$str = 'Fry me a Beaver. Fry me a Beaver! Fry me a Beaver? Fry me Beaver no. 4?! Fry
me many Beavers... End';
$sentences = preg_split('/(?<=[.?!])\s+(?=[a-z])/i', $str);
print_r($sentences);
Output:
Array
(
[0] => Fry me a Beaver.
[1]
[2]
[3]
[4]
[5]
=>
=>
=>
=>
=>
Fry
Fry
Fry
Fry
End
me
me
me
me
a Beaver!
a Beaver?
Beaver no. 4?!
many Beavers...
Explanation:
Well to put it simply we are spliting by grouped space(s) \s+ and doing two things:
1. (?<=[.?!]) Positive look behind assertion, basically we search if there is a point or
question mark or exclamation mark behind the space.
2. (?=[a-z]) Positive look ahead assertion, searching if there is a letter after the space, this
is kind of a workaround for the no. 4 problem.
<?php
$text1 = 'The quick brown fox jumped over the fence. Quick as a wink he was gone.';
// replace 'wink' with 'turtle'
$text2 = str_replace('wink', 'turtle', $text1);
// replace 'quick' with 'slow'
// NOTE</th><td>the 2nd 'Quick' is not changed
$textSlow1 = str_replace('quick', 'slow', $text2);
// replace 'quick' with 'slow' case insensitive
// NOTE</th><td>2nd 'slow' is lowercase!
$textSlow2 = str_ireplace('quick', 'slow', $text2);
// locate position of 'Quick' using strpos()
$pos = strpos($text2, 'Quick');
// split string using substr()
$string1 = trim(substr($text2, 0, $pos));
$string2 = trim(substr($text2, $pos));
// glue back together with replacements
// NOTE: we use "ucfirst()" instead of "ucwords()" to only replace 1st character!
$newText = str_ireplace('quick', 'slow', $string1) . ' ' . ucfirst(str_ireplace('quick', 'slow', $string2));
// display results
// NOTE: use 'single quotes' for text and "double quotes" if you need variables to be expanded
echo '<table border=1>';
echo "<tr><th>Original</th><td>$text1</td></tr>";
echo "<tr><th>Turtle</th><td>$text2</td></tr>";
echo "<tr><th>1st Replace</th><td>$textSlow1</td></tr>";
echo "<tr><th>2nd Replace</th><td>$textSlow2</td></tr>";
echo "<tr><th>Position Quick</th><td>$pos</td></tr>";
echo "<tr><th>String 1</th><td>$string1</td></tr>";
echo "<tr><th>String 2</th><td>$string2</td></tr>";
echo "<tr><th>New Text</th><td>$newText</td></tr>";
echo '</table>';
Debugging
//ini_set('display_errors', 0);
error_reporting(E_ERROR);
// 1. turn display of errors on
// NOTE: this should be turned off when done developing your application!
ini_set('display_errors', 1);
// 2. set error reporting to the highest possible level
error_reporting(E_ALL | E_STRICT);
as
$datum = Date("j/m/Y H:i:s", Time());
echo($datum);
POLE
$array1 = array('apple',2,'cucumber',True);
var_dump($array1);
echo '<br />';
echo "Element 0: {$array1[0]}";
echo '<br />';
echo "Element 1: {$array1[3]}";
$name['first'] = 'Tomas';
$name['last'] = 'Gajda';
$name['age'] = 25;
$name['funkce'] = 'podpora';
var_dump($name);
printf('<br />Full Name: %s %s', $name['first'], $name['last']);
Vcedimensionln Pole
Viz
arrays_multidimensional_1.php
arrays_multidimensional_2.php
echo '<pre>';
// create a 3 x 3 matrix
$matrix = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));
var_dump($matrix);
echo '</pre>';
echo "<br />Middle Element: {$matrix[1][1]}";
$arrayAssoc = array('first' => 'Julia', 'last' => 'Roberts', 'age' => 37, 'occupation' => 'Actress');
$foundKey = array_key_exists('last', $arrayAssoc);
$foundIsset = isset($arrayAssoc['last']);
echo '<br />Found by Key: ', $foundKey;
echo '<br />Found by isset: ', $foundIsset;);
Tdn pol
$array1 = array(1, 2, 3, 4, 5);
$array2 = array('first' => 'Julia', 'last' => 'Roberts', 'age' => 37, 'occupation' => 'Actress');
$length = array();
$length[] = count($array1);
$length[] = count($array2);
echo '<pre>';
echo '<br />1:';
print_r($array1);
echo '<br />2:';
print_r($array2);
echo '<br />Lengths:';
var_dump($length);
echo '</pre>';
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
// display results
echo '<br />Numeric: ', var_dump($arrayNumeric);
echo '<br /><br />Associative: ', var_dump($arrayAssoc);
echo '<br /><br />Files: ', var_dump($arrayFiles);
$arrayNumeric = array(1 => 11, 5 => 55, 2 => 22, 6 => 66);
// display results
echo '<pre>';
var_dump($arrayNumeric);
// remove element
$firstElement = array_shift($arrayNumeric);
echo "<br />Removed Element: $firstElement<br />";
// display results
echo "<br />WARNING: keys get reset using shift()<br />";
var_dump($arrayNumeric);
echo '</pre>';
/>";
$array1 = array('meat' => 'Beef', 'dairy' => 'Milk', 'grain' => 'Whole Wheat Bread', 'fruit' => 'Grapes');
$array2 = array('meat' => 'Pork', 'dairy' => 'Milk', 'grain' => 'Whole Wheat Bread', 'fruit' => 'Apples');
echo '<br /><br />Robraz co je v prnk navc:',
var_dump(array_diff($array1, $array2));
$array1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$array2 = array_slice($array1, 2, 5);
Vype :3,4,5,6,7
Spojovn pol
-co se kryje bude pepsno
$array1 = array('a' => 'Apples', 'b' => 'Oranges', 'c' => 'Bananas', 'd' => 'Grapes');
$array2 = array('d' => 'Kiwi', 'e' => 'Plums', 'f' => 'Pears', 'g' => 'Cherries');
$array3 = array_merge($array1, $array2);