VARIABLES
- Start with a dollar sign,for example : $variable_name=2;
- Do not need to be declared before use.
- Can hold a string, or a number, or a boolean: $a="cat"; $b=42; $c=false;
- Can store floating point numbers: $b=42.06;
STRINGS
- Can be assigned to a variable: $w = "world";
- Can be concatenated with the period (.) operator: "hello ".$w yields "hello world"
- The concatenation operator is used a lot. Remember it.
- You can get the length: strlen($w) returns 5
- You can get a substring: substr($w,1,3); returns orl
- Can be operated on by many PHP built in string functions
- Can be enclosed by double quotes (") or single quotes ('): $w = 'world';
- There are differences between single and double quotes.
- In double quotes, you can embed variables: "hello $w" produces hello world - the variable is substituted (its name replaced by its value)
- But if you try that with single quotes: 'hello $w' produces hello $w
- That same idea applies to special chars: "hello\n" produces a newline
- But 'hello\n' produces hello\n as a string (no newline) - taken literally with no substitution
- You can put a double quote in a double quoted sting by escaping: "Hello \"World\"" produces Hello "World"
- PHP strings act very differently than C character arrays. Much simpler to work with.
ARRAYS
- Create an empty one like this: $myarray=array();
- Create one with some elements: $myarray=array("cat","dog","hamster");
- Can hold a mixture of data types: $myarray=array("cat",42,false);
- Start with index of 0: echo $myarray[0]; // outputs: cat
- Unless the keys are specified: $myarray=array(1=>"cat",2=>42);
- And the keys don't have to be numbers, they can be strings: $myarray=array("fluffy"=>"cat","rover"=>"dog");
- Can append to an array like this: $myarray[]="a new element"
- Find out how many elements it has: count($myarray)
- Sort the array: sort($myarray)
- Sort the array by keys instead of values: ksort($myarray)
- Can be operated on by many built in PHP array functions.
FROM STRING TO ARRAY AND BACK AGAIN
- Here's a string: $s="Cat,Dog,Cow,Pig"
- Create an array from it: $a=explode(',',$s);
- $a[0] is now Cat, $a[3] is Pig
- Let's overwrite the Pig: $a[3]="Goose";
- Go back to a string: $s=implode(',',$a);
- $s is now: "Cat,Dog,Cow,Goose"
LOOPING
- foreach loops through entire arrays:
$myarray=array("fluffy"=>"cat","rover"=>"dog"); foreach ($myarray as $element) { echo $element; } // Outputs: cat, dog
- But you can also get at the array key values:
$myarray=array("fluffy"=>"cat","rover"=>"dog"); foreach ($myarray as $k => $element) { // We're using the concatenation operator here (period, remember?) echo $k." is a ".$element; } // Outputs: fluffy is a cat, rover is a dog
- There are other ways to loop, but foreach covers 90% of what you'll ever need.
IF-ELSE
- Looks just like if-else in C and other languages.
- Optionally, you can use a colon and endif statement, instead of braces:
// using curly braces: if ($w=="world") { echo "hello ".$w; // aside: note the concatenation operator (period) } // This is the same logic: if ($w=="world): echo "hello $w"; // aside: this time we didn't use concatenation! endif;
- There is a tertiary operator: echo ($w=="world") ? "hello $w" : "goodbye";
MIXING PHP WITH HTML
- Files ending with .php contain HTML and PHP
- PHP must be enclosed within PHP tags: <?php echo "I'm PHP";?>
- If it's not within PHP tags, it gets sent to the Browser unaltered.
- So whatever is not within PHP tags should be valid HTML/CSS/Javascript
- You can use the short PHP tag: <? echo "I'm PHP";?>
- For an echo, this is most concise: <?="I'm PHP"?>
- Works well for variables: <div class="<?=$classname?>">
I have read your web page and I got very useful and knowledgeable information about PHP Array and looping. It’s really a very nice information sharing for PHP developer. You have done a great job.
ReplyDelete