Sei sulla pagina 1di 9

echo var_dump($promena); Vype float(123.

45) nebo string(9) Some Text nebo bool(true)


$intValue = (int) $floatValue; Petypovn
isn/'t it? Vypsn specilnch znak
$promena /= 10; $promena = $promena / 10;

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));

printf('<br />Zanme od 4 znaku dle: %s', substr($name, 4));

$filename = __FILE__;
$extension = substr($filename, -8);
printf('<br />File Name: %s', $filename);
printf('<br />Extension: %s', $extension);

$name = 'Nejakej nesmyslnej retezec';


printf('<br />Zanme od 4 znaku dle: %s', sub str($name, 4));

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);

Zjitn pozice slova ve vt


$text = 'The quick brown fox jumped over the fence. Quick as a wink he was gone.';
$length = strlen($text);
$pos = strpos($text, 'Quick');
$ipos = stripos($text, 'Quick');
echo '<table border="1">';
printf('<tr><th>String </th><td>%s</td></tr>', $text);
printf('<tr><th>Length </th><td>%d</td></tr>', $length);
printf('<tr><th>Position sesitive</th><td>%d</td></tr>', $pos);
printf('<tr><th>Position insensitive</th><td>%d</td></tr>', $ipos);
echo '</table>';

Pevod z malch psmen na velk a naopak


$text = 'The quick brown fox jumped over the fence. Quick as a wink he was gone.';
// make lowercase
$textLower = strtolower($text);
// make UPPERCASE
$textUpper = strtoupper($text);
// make 1st letter Uppercase
$textFirstWords = ucwords($text);
// display the results:
echo '<table border="1">';
printf('<tr><th>lowercase </th><td>%s</td></tr>', $textLower);
printf('<tr><th>UPPERCASE </th><td>%s</td></tr>', $textUpper);
printf('<tr><th>1st Letter </th><td>%s</td></tr>', $textFirstWords);
echo '</table>';

$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']);

$ar = range(0,98,10); //Rychl0 naplnn pole start, stop, krok


$arrayAssoc = array('first' => 'Julia', 'second' => 'Tomas', 'third' =>'Lukas');
$arrayNum = array();
$arrayNum[2]=21;
$arrayNum[]=22;
$arrayNum[]=23;
var_dump($arrayNum);

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]}";

Vyhledvn (prohledvn) pol


$names = array('Doug', 'Julia', 'George', 'Francois');
$foundYesOrNo = in_array('George', $names);
$foundKey = array_search('George', $names);
echo '<br />Found George: ', var_dump($foundYesOrNo);
echo "<br />Found Key: $foundKey";
echo "<br />Retrieved Name: {$names[$foundKey]}";

$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>';

$arrayNumeric = array(22, 44, 11, 99, 88);


$arrayAssoc = array('a' => 'Apples', 'b' => 'Oranges', 'c' => 'Bananas', 'd' => 'Grapes');
$arrayFiles = array('file1.php', 'file2.php', 'file10.php', 'file20.php');
sort($arrayNumeric);
sort($arrayAssoc);
sort($arrayFiles);
// display results
echo '<br />Numeric: ',
var_dump($arrayNumeric);
echo '<br /><br />Associative
(NOTE: keys destroyed!): ',
var_dump($arrayAssoc);

echo '<br /><br />Files: ', var_dump($arrayFiles);


$arrayAssoc = array('a' => 'Apples', 'b' => 'Oranges', 'c' => 'Bananas', 'd' => 'Grapes');
$arrayFiles = array('file1.php', 'file2.php', 'file10.php', 'file20.php');
asort($arrayAssoc);
sort($arrayFiles, SORT_NATURAL);
echo '<br /><br />Using asort(): ', var_dump($arrayAssoc);
echo '<br /><br />Natural Sort: ', var_dump($arrayFiles);

<?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>";
}
?>

$arrayNumeric = array(22, 44, 11, 99, 88);


$arrayAssoc = array('a' => 'Apples', 'b' =>
'Oranges', 'c' => 'Bananas', 'd' => 'Grapes');
$arrayFiles = array('file1.php', 'file2.php',
'file10.php', 'file20.php');
rsort($arrayNumeric, SORT_NUMERIC);
arsort($arrayAssoc, SORT_STRING);
rsort($arrayFiles, SORT_NATURAL);

// NOTE: use flag if you know the nature of the data


// arsort() maintains keys

// display results
echo '<br />Numeric: ', var_dump($arrayNumeric);
echo '<br /><br />Associative: ', var_dump($arrayAssoc);
echo '<br /><br />Files: ', var_dump($arrayFiles);

$matrix = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));


array_multisort($matrix, SORT_DESC);
// NOTE: you can use SORT_ASC for ascending
array_multisort($matrix[0], SORT_DESC, $matrix[1], SORT_DESC, $matrix[2], SORT_DESC);
// display results
echo '<pre>';
var_dump($matrix);
echo '</pre>';

Odstraovn hodnot z pol


$arrayAssoc = array('meat' => 'Beef', 'dairy' => 'Milk', 'grain' => 'Whole Wheat Bread', 'fruit' => 'Grapes');
// display results
echo '<pre>';
var_dump($arrayAssoc);
// remove element
unset($arrayAssoc['grain']);
// display results
var_dump($arrayAssoc);
echo '</pre>';

$arrayNumeric = array(22, 44, 11, 99, 88);


sort($arrayNumeric, SORT_NUMERIC);
// display results
echo '<pre>';
var_dump($arrayNumeric);
// remove element
$lastElement = array_pop($arrayNumeric);
echo "<br />Removed Element: $lastElement<br />";
// remove element
$firstElement = array_shift($arrayNumeric);
echo "<br />Removed Element: $firstElement<br
// display results
var_dump($arrayNumeric);
echo '</pre>';

$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>';

U sel se resetuje indexy pol

/>";

$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);

Potrebbero piacerti anche