Friends Flags…

Free counters!

Sessions

No Sessions

UTF-8 Character Encodings in HTML and CSS via htaccess - Easy!

Years ago I fell in love with UTF-8.  More recently I have developed a love for HTML5 and CSS3.  I have found that as I am teaching these, my students have been confused about how to set Character Encodings (and Content-Language).  So I decided to make an easy to follow tutorial on how to set both of these in your htaccess file.

Continue reading UTF-8 Character Encodings in HTML and CSS via htaccess – Easy!

PHP Tutor #4 - Fixing a String Case, trim, ucwords, strtolower

In this tutorial we will explore manipulating the case of a string, and how to clean up strings like names. in PHP Tutor #4 we learned how to put a literal string into a variable like this:

<?php
  $name = 'marie estebanie pelin';
?>

However, the problem with this is, it is all lower case! And what if the person entering it had added a space at the front or end of the string? That could mess up alphabetizing a list! Or what if we wanted to extract the initials, or what if we wanted just the first two letters of each name to make some kind of username? Let’s look at how we can manipulate the case as well as extract a few things from a Crazy Bird string. Assume someone entered the following. Notice the extra space at the end!

<?php
  $crazy_bird_name = 'mArIe eStEbANIe pELIN ';
?>

Trim White Spaces

You can trim white spaces (or any character) from the left, right or both ends of a string!

Let’s use trim or rtrim to get rid of the white spaces at the right.

<?php
 $crazy_bird_name = 'mArIe eStEbANIe pELIN ';
 $crazy_bird_name = trim($crazy_bird_name);// now it's = 'mArIe eStEbANIe pELIN'
?>

So now there is no space!

More Ideas:

  • ltrim() – Strip whitespace (or other characters) from the beginning of a string
  • rtrim() – Strip whitespace (or other characters) from the end of a string
  • str_replace() – Replace all occurrences of the search string with the replacement string

 

Start a Word with a Capitol Letter

<?php   $crazy_bird_name = 'mArIe eStEbANIe pELIN ';
  $crazy_bird_name = trim($crazy_bird_name); // now it's = 'mArIe eStEbANIe pELIN'
  $crazy_bird_name = ucwords($crazy_bird_name); // now it's = 'MArIe EStEbANIe PELIN'
?>

Two Lines Only

This could have been written with only two lines of code by combining the last two lines like this:

<?php   $crazy_bird_name = 'mArIe eStEbANIe pELIN ';
  $crazy_bird_name = ucwords( trim( $crazy_bird_name ) ); // now it's = 'MArIe EStEbANIe PELIN'
?> 

 

More Ideas:

Not Quite Right!

The above did make each word start with a uppercase letter, but it did not make them look like we wanted, human names! What we really wanted was for the first letter of each word to be uppercase and the rest of each word to be lowercase.  So here is how we can do that…

<?php
 $crazy_bird_name = 'mArIe eStEbANIe pELIN ';
 $crazy_bird_name = trim($crazy_bird_name); // now it's = 'mArIe eStEbANIe pELIN'
 $crazy_bird_name = strtolower($crazy_bird_name); // now it's = 'marie estebanie pelin'
 $crazy_bird_name = ucwords($crazy_bird_name); // now it's = 'Marie Estebanie Pelin'
?> 

We can also write this shorter like this…

<?php
 $crazy_bird_name = 'mArIe eStEbANIe pELIN ';
 $crazy_bird_name = ucwords( strtolower( trim( $crazy_bird_name ) ) ); // now it's = 'Marie Estebanie Pelin'
?> 

 

 

PHP Tutor #3 Operators and Assigning Variables

One of the very first things you are going to want to do is learn where are the php operators! Learn this well, Google

PHP operators

In the Google Results Page, look for the website:

www.php.net

And click there!

You should find yourself On the Operators page of the PHP Manual.

http://www.php.net/manual/en/language.operators.php

You will be looking at a list of links to the various PHP Operators.

In the Beginning…

As you start to learn php, you will use this page a lot. Operators are used in most lines of code. I tell you this so you will take note of how to get back to the Operators page, because you will be coming back here (after a decade and a half of coding php, I still come back here some days!).

Assigning a Simple Variable

There is a basic assignment operator, it is “=” (the “equal sign” without the quote marks!). It can be modified a number of ways with other operators, but we will leave that for another lesson.

You may be tempted to think of = as being “equals”, but that is very dangerous in conditions! What you should do is think of this assignment operator as “gets set to” instead of “equals”. When you get to conditional operators you will see that == means “equals”, so better get used to “=” meaning “gets set to” from the start!

Marie’s $name

My apprentice name is Marie Estebanie Pelin, so lets set her name in a variable name $name.

<?php
  $name = 'marie estebanie pelin';
?>

Remember to read this as:

$name gets set to 'marie estebanie pelin';

Always Use Single Quotes…

A good rule I live by is, “Always use single quotes, unless you MUST use double quotes!” It will save you in debugging time! If you do not know what to use, it is a safe bet you likely need single quotes.

Normal HTML5

For this I am using The real HTML5 boilerplate, so the HTML5 looks like this…

<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <title>HTML5 boilerplate – all you really need…</title>
    <link rel="stylesheet" href="css/style.css"> 
      <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
      <![endif]-->
  </head>
  <body id="home">
    <h1>HTML5 boilerplate</h1>
</body>
</html>

Inserting PHP into HTML

Now let’s add the PHP so that both the title and the body <h1>tag display Marie’s name.

<?php // marie_name.php
  $name = 'marie estebanie pelin';
?>
<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <title><?=$name;?></title>
    <link rel="stylesheet" href="css/style.css"> 
      <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
      <![endif]-->
  </head>
  <body id="home">
    <h1><?php echo $name; ?></h1>
</body>
</html>

Less Typing

After PHP v5.4 the following two commands are identical. This is because the echo shorthand has many uses. Before v5.4 you should make sure short-tags are turned on in PHP.  If you are not sure, the one with the word echo in it is likely safer at this time.  But as servers get updated to v5.4 the shorter one will see more and more usage.  They both work fine on most systems now.

<?=$name;?>
<?php echo $name; ?>

Either one echos (outputs) the data within the Variable.

Wrap-Up

In this lesson you learned how to put a literal string into a Variable and how to late echo it back to the browser screen.

PHP Tutor#2 - Setup Your Workspace

The first tool you will need for learning PHP is a workspace.  A place you can write and test your code.  That is usually two work spaces, the first being a quality text editor and the second is a way to host your code (and test it) on a server.

 

Not a Word Processor

A Text Editor is not a Word Processor.  We who have made the mistake to every having touched a Windows computer, may remember notepad.  Well, that is a Text Editor.  On the other hand, Word from the Microsoft Office Suite is NOT a Text Editor.  Do not try to write code on Word – it will just give you headaches and sleepless nights.

Those smart people who use and support Ubuntu should not try to use LibreOffice Writer, that like Word (only better) LibreOffice Writer is a Word Processor.  The built in Text Editor in Ubuntu is called gedit, which is actually pretty (a lot better than notepad).

 

I Dream of Geany

If you want my very best, #1, Top Recommendation for a Text Editor, use Geany (I do!).  Geany has a lot of plugins and customization you can use for the type of coding you do, and built in php support!

Continue reading PHP Tutor#2 – Setup Your Workspace

PHP Tutor #1 - The Need and Some Structure!

My HTML apprentice, Marie, wants to learn php. She has a logical command of her thoughts, and a degree in accounting, from the University of Cebu. I figured she would take to the abstract thinking of php fast enough because accounting uses abstract thinking (algebra) and because Marie picked up HTML faster than any other new student I have had (a student starting from scratch).

This is an ongoing Tutor for PHP for people with no other knowledge – so it is very simple, and points out the pitfalls and mistakes most Newbies make early on (and some experienced programmers make too!). Each of these Tutorials will cover very small steps in learning PHP with a focus on using the PHP manual, and some ideas on how you can use certain program constructs and functions in your code.
Continue reading PHP Tutor #1 – The Need and Some Structure!

SEO and Well Formed Code Validation

You can find a lot of information on the Internet about SEO or Search Engine Optimization.  Sadly, most of it is wrong information that one so called “expert” copies from another so called “expert” who had just copied it from another so called “expert!”

They almost always say something like, “it works for me!” But rarely do they have any insight into WHY it worked for them. They will use a lot of buzz words like “page rank”, “SERP”, “keywords” and one of my favorites, “stop words!”

Although with Google Hummingbird, “keywords” should be replace with “key phrases”, each of these have there place.
Continue reading SEO and Well Formed Code Validation

To www or Not to www - Is that the Question? SEO and Green Websites!

Once upon a time,, way back in Nineteen-Forgotten, there was a need for WWW, Those little four characters (do not forget the dot!) were added not as a romantic way to show people it was a website on the World Wide Web. It was actually added for a practical DNS reason most people under 40 don’t know, in-addr.arpa. Today we call it reverse DNS. You see, back then we had not learned how to forward and reverse DNS to the primary Zone. Today we do, so today the www. is just a waste of energy, space and time.

For those wanting to be more green, www. increases the carbon footprint of your website!
Continue reading To www or Not to www – Is that the Question? SEO and Green Websites!

Dynamic WordPress Sitemaps with PHP

As they say, it is the little things that mean so much.  Like how to add a human readable dynamic title to a machine generated Google sitemap.  That is just what I am about to show you using a simple little modification anybody with a little edit skills can do.  Best of all you can even do this on your Multisite WordPress install, and ALL domain names will benefit.

Continue reading Dynamic WordPress Sitemaps with PHP

2 SEO Tips for Beginners - Understanding Sitemaps

Understanding Sitemaps: What are the two most important things you need to know about Sitemaps? As a teacher I often need to provide answers to questions like these. Many people today still do now know what a sitemap is or how you can read one. This will teach you the basics you NEED TO KNOW about Sitemaps and why you need one.

A search engine is so called because it searches the Internet. It starts off with a list of domain names. Names like aLanTait.Net or AngBarato.Com.  It goes to these domains and fetches the “Landing Page!” From there is crawls or spiders to other places by following the links.  So you need to have a link to any page that is not the landing page for your domain.  Search Engines like Google and Bing cannot find stuff without a link.  No link, No Index!  A Sitemap helps Google and Bing Find your stuff!

Continue reading 2 SEO Tips for Beginners – Understanding Sitemaps

SEO Meta Descriptions - The Essential Guide

Essential SEO is about people who “Click Through” and look at your site. This guide is about using Meta Descriptions for SEO!  Good rank is not enough.

Many of the so called SEO experts focus solely on getting the highest position on the SERP (that would be Search Engine Results Page, not licking an ice cream!).  The problem with this thinking is that it focuses on getting the position on the page or the impression.  What really matters is the Click Through!

 

Continue reading Meta Descriptions for SEO!