php Code Snippets

Quotes, Double Quotes, echoing html


to use double quotes in the markup
do like so
echo '<a href="' . $uri . '">' . $text . '</a>';
echo "<a href=\"$uri\">$text</a>";

Redirect

You can see it in action here


<?php header("Location: http://bowdenweb.com"); ?>

.htaccess Codes

.htacess Rediects


ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

Change .php URLs in .html

If you have all the pages of site in php, after writing this single line code in .htaccess, they will be changed into .html. I mean the URLs will be changed to .html. Link your pages with .html extension and write this code in .htaccess file and you are done.

credit - Htaccess Mod Rewrite Essential Important Solutions


RewriteRule ^(.*)\.html$ $1.php [NC]

php and Simple Mathematics

Summation

16 + 30 = 46


<p>16 + 30 = 
<?php
$sum = 16 + 30;
echo $sum;
?>

Here's one with multiple variables: what is the sum of (a = 26) + (b = 30)? 56 should echo out before the word should, and it should be 46.


<p>Here's one with multiple variables: what is the sum of (a = 26) + (b = 30)?
<?php
$a = 26;
$b = 30;
$sum = $a + $b;
echo $sum;
?>
 should echo out before the word should, and it should be 56.</p>

Multiplication

Multiplying 26*30*35 = 27300 it should be 27300.


<p>Multiplying 26*30*35 = 
<?php
$a = 26;
$b = 30;
$c = 35;
$multiply = $a*$b*$c;
echo $mutiply;
?> it should be 27300.</p>

Subtraction

Subtracting 30 -26 = 4 and your answer should be 4.


<p>Subtracting 30 -26 = 
<?php
$a = 26;
$b = 30;
$sub = $b-$a;
echo $sub;
?> and your answer should be 4.</p>

Division

Divide 30/26 = 1.1538461538462 answer should be 1.1538461538461537.


<p>Divide 30/26 = 
<?php 
$a = 26;
$b = 30;
$divide = $b/$a;
echo $divide;
?> answer should be 1.1538461538461537.</p>

Exponential Expression

pow ($base, $exp) where base is raised to the power of exp.

What is 26 to the 30th power? 26 to the 30th power is 2.8131989012847E+42 .


<p>What is 26 to the 30th power? 26 to the 30th power is 
<?php 
$a = 26;
$b = 30;
$power = Pow($a, $b);
echo $power;
?> .</p>