Ann's Corner

 
   
   

Professional Life

Resume
Databases 
Adobe Photoshop
Computer Help
Education
Cool Code
 

PHP Code

PHP is a very versatile, easy to use language.  Here are a few of my most often used snippets of code.

Sometimes you will need to know what your host is running and what you have access to using PHP.  To find out, create a file named something like "phpinfo.php" and save it in the root directory of your site.  Put the following code snippet at the top of this file, save it and access the page with your browser.

//php info
<?php echo phpinfo(); ?>

Any variable you create can be echoed onto any page.  For instance, to personalize a greeting for each user simply create an html form on a page, set the forms' action to post to itself, then call the variable you just posted.
For this example we will call the page whatever.php.  So on your whatever.php page where you want the form to appear, use this code:

<form name="form1" method="post" action="<?php echo "$thisfile";?>">
<input name="name" type="text" id="name">
<input type="submit" name="Submit" value="Submit">
</form>

Now at the very top of whatever.php, enter this code:

<?php if($_POST['Submit']=='Submit'){
$name = $_GET['name']; }
?>

On your page where you want the personalized greeting to appear, enter this code:

<?php echo 'Welcome back' $name; ?>             
         

What this does is each time the page loads, it will check to see if the variable "name" is in the url.  If it isn't, nothing happens, but if it is, whatever value the user entered into the form "name" box will be echoed back to the page.

The last useful php snippet I'll post here is about manipulating dates.  This snippet will echo the current date and time wherever you put it on your page.

<?php
// Prints something like: Wednesday
echo date("l");

// Prints something like: Wednesday 15th of January 2003 05:51:38 AM
echo date("l dS of F Y h:i:s A");

// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000)); ?>

 

   

Hobbies

Genealogy
History
 
   

Photography

Landscapes
Birds
Nature
Flowers
Miscellaneous
Links
 
   
Copyright 2008 Ann Carrier
You are the 175787 visitor.
Bird Photos Landscape Photos Flower Photos Anns Corner Home Nature Photos