News & Events

News, Events and other cool stuff.

  • 06/10/15 Uncategorized # ,

    Basic PHP Functions

    // string
    $variable = "The quick brown fox jumped over the lazy dog.";
    
    // functions
    Lowercase: <?php echo strtolower($variable); ?>
    Uppercase: <?php echo strtoupper($third); ?>
    Uppercase first: <?php echo ucfirst($variable); ?>
    Uppercase words: <?php echo ucwords($variable); ?>
    
    Length: <?php echo strlen($variable); ?>
    Trim: <?php echo "A" . trim(" B C D ") . "E"; ?>
    Find: <?php echo strstr($variable, "brown"); ?>
    Replace: <?php echo str_replace("quick", "super-fast", $variable); ?>
    Repeat: <?php echo str_repeat($variable, 3); ?>
    
    Make substring: <?php echo substr($variable, 5, 10); ?>
    // starts at position 5
    // then goes 10 spaces
    Find position: <?php echo strpos($variable, "brown"); ?>
    Find character: <?php echo strchr($variable, "z"); ?>
    
    Basic Math: <?php echo ((1 + 2 + $variable) * $var2) / 2 - 5; ?>
    Absolute Value: <?php echo abs(0-300); ?>
    Exponential: <?php echo pow(2, 8) ?>
    Square Root: <?php echo sqrt(100); ?>
    Modulo: <?php echo fmod(20, 7); ?>
    Random: <?php echo rand(); ?>
    Random (min, max): <?php echo rand(1,10); ?>
    
    // this is the shorthand version of $var2 = $var2 + 4;
    += : <?php $var2 += 4; echo $variable; ?>
    -= : <?php $var2 -= 4; echo $variable; ?>
    *= : <?php $var2 *= 3; echo $variable; ?>
    /= : <?php $var2 /= 4; echo $variable; ?>
    Increment: <?php $var2++; echo $variable; ?>
    Decrement: <?php $var2--; echo $variable; ?>
    Round: <?php echo round($variable, 1); ?>
    // the 1 specifices decimal place
    // ceiling rounds up, floor rounds down
    Ceiling: <?php echo ceil($variable); ?>
    Floor: <?php echo floor($variable); ?>
    
    <?php $numbers = array(8,23,15,42,16,4); ?>
    
    // How many items are in the array
    Count: <?php echo count($numbers); ?>
    // what is the highest integer
    Max Value: <?php echo max($numbers); ?>
    // what is the lowest integer
    Min Value: <?php echo min($numbers); ?>
    
    // sort from least to greatest or ABC Order
    <pre>Sort: <?php echo sort($numbers); print_r($numbers); ?></pre>
    // sort from greatest to least or ZYX
    <pre>Reverse Sort: <?php echo rsort($numbers); print_r($numbers); ?></pre>
    
    // add these characters between each item and convert to string
    Implode: <?php echo $num_string = implode(" * ", $numbers); ?>
    // turn string text between these characters into array items
    Explode: <?php print_r(explode(" * ", $num_string)); ?>
    
    // returns true or false
    15 in array? : <?php echo in_array(15, $numbers); ?>
    19 in array? : <?php echo in_array(19, $numbers); ?>
NO NEWER ENTRIES
NO OLDER ENTRIES
Recent Posts
Recent Comments
    Archives
    Categories