Php exercises help please

Now starting php exercises from here:

PHP Beginnings Ex. #5: Variable Content and Destruction | PHP Exercises

I am not understanding the purpose of this exercise, can anyone tell me in which real application this would be useful please?

It’s trying to show you… that you can output variable content without ECHO? I guess?
He’s also not destroying the variable - should be using unset() for that. The ‘error’ reported in the contents is the correct thing - the variable shouldnt exist anymore.

Not…really the best tutorial.

anyway. Why would you need this information? Debugging, mainly. var_dump’ing a variable is good for making sure the variable contains what you believe it should contain.

Well, it’s not a tutorial, just a bunch of exercises for me to test myself but thanks for the explanation. I don’t understand one thing here:

$around = "around";
echo 'What goes ' . $around .  ' comes ' . $around . '.' ;

what if you don’t want a period at the end of the string like this:

What goes around comes around

why does it NOT work if you remove the ‘.’ at the end of the code so it is now?

echo 'What goes ' . $around .  ' comes ' . $around . ;

Because you are telling PHP to concatenate the var $around to nothing, which is an error, remove the last . and it will work again.

ok, how would I add a comma after the word around please so it would show as

what goes around, comes around

??

Give it your best shot, and post your attempt here.

I have come up with this but it still does not place the comma at the correct spot which is next to the ‘d’ [last letter in the word ‘around’], if you notice the comma is one space away:

$myWord = 'around';
echo 'What goes ' . $myWord .  ', comes ' . $myWord;  

“What goes around, comes around”

Yes it does. Don’t worry about white space around the concat dots, all of these will create the same result.


echo 'What goes ' . $myWord .  ', comes ' . $myWord;


echo 'What goes '.$myWord.', comes '.$myWord;


echo "What goes $myWord, comes $myWord";


echo 'What goes ' , $myWord ,  ', comes ' , $myWord;


echo 'What goes '                .$myWord.          ', comes '.$myWord;

yes, it works, thanks for help!

When using this code:

$date=getdate(date("U"));

if($date[month]=="August")echo "Its August, so its really hot.";
else echo "Not August, so at least not in the peak of the heat.";

The output is :

Notice: Use of undefined constant month - assumed ‘month’ in C:\xampp\htdocs\index.php on line 9
Not August, so at least not in the peak of the heat.

What’s wrong here please?

You need to quote the array key.


<?php
$date = getdate();
echo $date['month'];

It’s often accepted practice to use [fphp]date[/fphp] for this, but you may have other reasons why you’re using getdate. :slight_smile:


<?php
echo date('F');

$date[‘month’] <-this is accessing an array, quote the keys when they are strings - or you get the nasty warning.

$date[0] <-this is accessing an array, no need to quote integers

Regarding the exercise:


abc abc abc abc abc abc abc abc abc

xyz xyz xyz xyz xyz xyz xyz xyz xyz

1 2 3 4 5 6 7 8 9

    Item A
    Item B
    Item C
    Item D
    Item E
    Item F

Create the 'abc' row with a while loop, the 'xyz' row with a do-while loop, and the last two sections with for loops. Remember to include HTML and source code line breaks in your output. No arrays allowed in this solution.

I am not understanding a few parts of the code:

[B]echo "<p>\
";[/B]
 
$counter = 1;
while ($counter < 10){
  echo 'abc ';
  $counter++;
}
 
[B]echo "</p>\
";
echo "<p>\
";

[/B]

What’s the bolded parts of the code for please?

<p> is the HTML code for a paragraph block.

is an escape sequence to put a newline character in the source code, to make it readable.

Thanks, exactly, first time I am seeing the
, I was wondering how the output was showing on separate lines without the <br/>

the -output- is being broken up by the <p>'s.
the -source code- gets broken up by the
's.

Yeah, realizing that now, thanks!