I know i already explained Strings a bit but that was only basic stuff.
I will explain the usage of strings and will make some more programs while using
string functions.
how do we make a string?
you can create them in 2 ways
using the
" or the
' sign
$string1 = "some text";
$string2 = 'some text';if you type only text inside there is no difference in them
BUT while working with other
variables and/or strings the difference is huge
while that difference is sometimes useful in most cases it causes bugs because
of the programmers carelessness.
those signs are not only used with strings but also with the echo function (and
some others)
to show the difference i will create a little php script:
1.) combine them with the echo function to print out a message
$string1="my";
$string2="car";
$string3="Tom";
echo "$string3 has $string1 $string2";
echo "\$string3 has \$string1 \$string2";
echo '$string3 has $string1 $string2'; #line6
?>
The output will be:
Tom has my car
$string3 has $string1 $string2
$string3 has $string1 $string2
as you can see the only difference is the
" and the
' sign .
while using the
" sign the echo function searches for the variables $string1 , $string2
and $string3 and prints them out (if they are not declared before nothing they
will be ignored and the rest will be printed out) like shown on line 4 of the
code
if you don't want to print out the value, only the name of the variable while
using the
" sign just put a backslash\
before the variable like shown on line 5
now let's check out the line 6...
the echo on line 6 will see all between the
' sign as normal txt and will print out
that is useful when you don't want to put backslashes all the time and if you
use more txt of the variables than the value inside it
what if you need both the same amount?
well the we just combine them using the Concatenation Operator (.)
how does it work?
let's show it with a example:
$Tom="Tom";
$Boy="boy";
$Girl="girl";
$Claire="Claire";
// example with the " sign
echo "$Tom is a $Boy that likes a $Girl called $Claire";
// example with the ' sign
echo $Tom . ' is a ' . $Boy . ' that likes a ' . $Girl . ' called ' . $Claire . '';
// both mixed
echo 'The string $Tom has the value:
' . $Tom . "";
echo 'The string $Tom has the value:' . "$Tom";
You can also use the Concatenation Operator (.)
to combine strings (shown in the previous Course).
Strings are mostly used for storing some temporary information and is combined
with other functions but mostly is used with files and databases
(more later on when we come to files)
That's it for now with strings.
if you have any questions or want to add some more info just ask or write it
here down.
