Thursday, March 8, 2012

Getting Started with PHP: Strings

Strings


Strings are lines of text characters:
"AwesomeDoni loves books"
"It's raining outside today"
These are two examples of strings. In PHP, these are indicated by quotations after a command. It's useful because you can simultaneously declare a variable and its value while commanding PHP to perform a function, enclosing variables to be displayed as text as single quotes.





For example:

<?php $userAdmin = "AwesomeDoni" ; print 'This web site is managed by $userAdmin.'; ?>
Again, I've indicated the variable by the yellow
box, the value is the green ball inside, "AwesomeDoni".
Again, you can see how PHP interprets the variable based upon it's value, indicated by the quotation marks (" ") and outputs data according to rules we've set up with the help of single quotation marks (' ').

In this case, we've told PHP to print some text before it outputs the variable that we've declared. This is an example of simple programming functionality. The program follows a set of instructions, reads data according to the way that we've organized it, and performed an action based upon this organizational structure. 


In order to display a variable, you must use single quotes. Notice that any data within quotations remains unseen to the user and does not appear in the browser.


Formatting Strings


Currently, there are three rules for formatting strings in PHP. These may be useful when you organize your own code for other programmers. Remember my article on documenting PHP code? You can make your own work practically useless if you fail to consider proper organizational techniques. Formatting strings in PHP does not affect the appearance of text in the browser, but will affect the appearance of code. You can style the output of PHP code using XHTML, which is what makes PHP so useful when creating your own web pages.


  • \n - creates a new line in code
  • \t - tab
  • \$ - allows you to add an extra $ without disrupting your code by mis-declaring a variable
  • \ - tells PHP not to interpret a quotation as a string
For example:

<? php $userAdmin = "AwesomeDoni" ; print '$userAdmin has one rule \"Please don\t break it\."
Appears as:

AwesomeDoni has one rule "Please don't break it."
This keeps your PHP from breaking, and you can still continue to style it using XHTML.

Commands

Using strings, you can iterate and perform functions on complex sets of variables.
  • print - reiterates a string of text in the browser 
  • echo - reiterates simple text
Use print when you want to command PHP to output complex strings of text onto the browser. Use echo when you want PHP to output a simple word or symbol.

So far, we've talked about storing a single piece of data inside of a variable, but how do programmers work with variables that hold many different types of data--multiple values? We'll discuss that in our next section, but for now you've got the basics of how PHP reads, interprets and handles data based upon simple sets of programming instructions.

No comments:

Post a Comment