To get further than the tutorial, I'm now going through the somewhat dated 2004 book, Beginning PHP5 by Mercer, et al. So yay, more random posts on stuff I'm learning. I know you're so excited! LOL
Chapter 2 of this book deals with writing simple programs. A lot of the first bit I just skimmed because it was really elementary stuff. The second half gets into variables and operators. Obviously I covered some of this in the tutorial, but the book goes a bit more in-depth.
First, strings. of course, I already did the basic concat operator and looked at strlen() and strpos(). From the book, though, I learned two new tricks: you can use the contact operator in short form, like the + thing and using the strstr() function.
<?php $greeting = "My name is ";
$name = "Inigo Montoya";
$greeting .= $name;
echo $greeting; echo "<br />"; echo "Last Name: " . strStr($name, " ");
?>
From there it goes into arithmetic operators. The basics are kind of "well duh", but it does also go into the special assignment operators (i.e. what I called short form). In particular, it's important to note that the placement does matter!
<?php $aNumber = 5;
$anotherNumber = 10;
echo "<strong>Numbers are 5 and 10</strong><br />";
$aNumber += $anotherNumber;
echo "<strong>Add:</strong> " . $aNumber . "<br />"; $aNumber -= $anotherNumber;
echo "<strong>Subtract:</strong> " . $aNumber . "<br />"; $aNumber *= $anotherNumber;
echo "<strong>Multiply:</strong> " . $aNumber . "<br />"; $aNumber /= $anotherNumber;
echo "<strong>Divide:</strong> " . $aNumber . "<br /><br />";
echo "<strong>Base number is</strong> 5<br />"; $anotherNumber = 5;
$test1 = ++$anotherNumber;
echo '<strong>++5</strong> = ' . $test1 . '<br />'; $anotherNumber = 5;
$test2 = $anotherNumber++;
echo '<strong>5++</strong> = ' . $test2 . '<br />'; $anotherNumber = 5;
$test3 = --$anotherNumber;
echo '<strong>--5</strong> = ' . $test3 . '<br />'; $anotherNumber = 5;
$test4 = $anotherNumber--;
echo '<strong>5--</strong> = ' . $test4 . '<br />';
?>
Then to arrays again. Mostly picking up more array functions here versus the tutorial.
<?php $mybooks = array();
$mybooks[] = "D.N.Angel, Volume 12";
$mybooks[] = "Spice & Wolf, Volume 07: Side Colors";
$mybooks[] = "Kieli, Volume 07: As the Deep Ravine's Wind Howls";
$mybooks[] = "Book Girl and the Corrupted Angel (Book 4)";
$mybooks[] = "Ultra Cute, Volume 09";
$mybooks[] = "D.N.Angel, Volume 12";
echo "I have " . count($mybooks) . " books in my array.<br />";
if (count(array_count_values($mybooks)) != count($mybooks)) {
echo "There is a duplicate entry in my array....fix please, or better yet I will. Here's what I had:<br />";
echo "<pre>";
print_r (array_count_values($mybooks));
echo "</pre>";
$mybooks = array_unique($mybooks);
}
$pets = array();
$pets["Border Collie Mix"] = "Yuki";
$pets["Australian Shepherd Mix"] = "Blue";
$pets["Domestic Medium Hair"] = "Fusion";
echo "Pets:<br />";
echo "<pre>";
print_r ($pets);
echo "</pre>"; echo "And flipped it<br />";
echo "<pre>";
print_r (array_flip($pets));
echo "</pre>";
echo "ASorted...";
echo "<pre>";
asort($mybooks);
print_r ($mybooks);
echo "</pre>"; echo "vs regular Sorted...";
echo "<pre>";
sort($mybooks);
print_r ($mybooks);
echo "</pre>";
?>
Count unique values is cool, especially since I quickly realized I could use it as I did in my example to indicate a duplicate value has been found. Array_flip() is interesting though I'm not sure what practical usage it will have. ASorted is a good one to remember since it keeps the index values!
Note: I've realized that there is a bug in the SyntaxHighlighter causing it to blank out the BR tags from my code. So if you see echo "" it is supposed to be echo "<br /><br />" and many of the other blank "" at in the concatenated strings are actually "<br />". So far I haven't found a fix as the code, and while this bug was report on the developer's GitHUb 2 years ago, he has never responded.