Toucan Flock
Would you like to react to this message? Create an account in a few clicks or log in to continue.

PHP Programming 102

Go down

PHP Programming 102 Empty PHP Programming 102

Post by Kaeliah Wed Mar 27, 2013 4:26 pm

[PHP 101]

This is the second PHP tutorial, if you haven't seen the first one use the link ahead. I will be assuming you understand the concepts from the previous tutorial. Smile

Strings & Concatenation
In the previous tutorial we talked a little about strings, but here we're going to go more in depth. Strings are variables that contain letters, numbers and other symbols often used in speech. It's used for outputting text, or you can get pretty crazy with strings if you want to. We'll keep it simple though. Concatenation(con-cat-en-ay-shun) is a big fancy word for sticking two strings together. Sort of like adding them. String variables look like this:


Code:

$string = 'text here!';

The $string is the variable that will hold our text, we then use the equal sign to assign the text to the variable. Whenever you're dealing with strings always make sure you have either SINGLE or DOUBLE QUOTES around the text. I recommend single quotes, but I'll explain why in another lesson. Obviously having these quotes can be a pain because what if you have a word on the inside of your string that needs an apostrophe, or other quotes? We'll use something called escaping to avoid this problem.

Code:

$string = 'I wish to quote this person, he said \'Hello World!\' ';

// Will output: I wish to quote this person, he said 'Hello World!'

Looks a bit funny, but in essence we use the back slash \ to escape the quote. When you escape the quote, the computer ignores that quote and continues to count what comes after it as part of the string. This is an important tip to know. You can also escape double quotes, and escape the back slash itself by adding another one before it ( \\ ). Placing a double back slash in a string, when outputted you'll only see one back slash.

Now on to Concatenation. Concatenation is an operation done on strings using the . operator. It looks something like this:

Code:

$string = 'World!';
echo 'Hello ' . $string;

Notice the period before the $string, saying we are adding that text onto the end of what we are already outputting. The result will look like Hello World!, nice and smooth. You can also concatenate many variables together by simply separating them with a period. If you wanted to, you could also place $string in the front of 'Hello' and add a period behind $string to concatenate that way. I like to think of it as blocks that are being connected, like legos.


Operators
Operators is the last topic for Programming 102, because it's kind of a big one. There are tons of operators in programming languages, both mathematical and logical. To sit here and try to explain each one would take forever, so instead I'm going to pick the most popular and simply leave you with a link if you want to see ALL of them.

Math Operators
Operator Name Function Example Input Example Output
$x + $y Addition Sums two values together. 4 + 5 9
$x - $y Subtraction Subtracts $y from $x.
$x * $y Multiplication Multiplies two values together.
$x / $y Division Divides $x by $y
$x % $y Modulus Finds the remainder of $x divided by $y.
- $x Negation Turns from positive to negative or
negative to positive.
-3
5
3
-5
$x . $y Concatenation Combines two strings. "Ya"."Hoo" "Yahoo"


Work in progress...
Kaeliah
Kaeliah
Community Advisor
Community Advisor

Posts : 43
Beak Points : 4091
Reputation : 5
Join date : 2013-03-26
Age : 31
Location : Millersville, Pennsylvania

http://www.createadopt.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum