Jump to content
Sign in to follow this  
kuba2

PHP codeacademy problem ( do/while )

Recommended Posts

Hello

I am learning PHP and made a codeacademy PHP online course. Basic stuff and interesting course for free.

But there is a do/while exercise I can't get my head around:

 

 

In the following, there is a flipcoin example, which flips, as long the result is head.

In the do statement there is the if and the else 

The if condition is $flip and it echoes a H letter for Head. The else echoes a T for Tail.

$flip is random 0 or 1

My question:

How can the if and else statements be H or T if $flip is random? I don't understand the $flip variable, because in my head it can be 0 or 1.....therefor if is not one fixed value, because of the rand(0,1) it can change

How does the programm to put out a H or a T?

I hope my question is clear enough

 

 

Thanks for any clarification

Jakob

<!DOCTYPE html>
<html>
    <head>
    	<link type='text/css' rel='stylesheet' href='style.css'/>
		<title>More Coin Flips</title>
	</head>
	<body>
	<p>We will keep flipping a coin as long as the result is heads!</p>
	
	
	<?php
	$flipCount = 0;
	do {
		$flip = rand(0,1);
		$flipCount ++;
		if ($flip){
			echo "<div class=\"coin\">H</div>";
		}
		else {
			echo "<div class=\"coin\">T</div>";
		}
	} while ($flip);
	$verb = "were";
	$last = "flips";
	if ($flipCount == 1) {
		$verb = "was";
		$last = "flip";
	}
	echo "<p>There {$verb} {$flipCount} {$last}!</p>";
	?>
	
	
    </body>
</html>

 

Share this post


Link to post
Share on other sites

@kuba2,

Moderator Note

I have moved your topic to  Dev Talk since it has nothing to do with ProcessWire per se.

  • Like 1

Share this post


Link to post
Share on other sites
if($flip) {
  echo "H"
}

You are exactly right, I think you just miss a thought:

The condition relates to the value of $flip. The value is either 0 or 1. And in PHP, 1 in a condition is “like” true, and 0 is “like” false.

So $flip may be 1. In that case PHP will coerce it to true and finally print H because the condition is met.
If $flip is 0, PHP will coerce that to false and print T because the condition for the if() was not met. So the else block is executed.

Hope that helps?

  • Like 7

Share this post


Link to post
Share on other sites

In addition to what blynx said:

if ($flip) 

means the same as

if ($flip!=0)

 

  • Like 3

Share this post


Link to post
Share on other sites

Just wanted to add that it's not just PHP that treats 0 as False and 1 as True. 
You'll find most (traditional) languages will do the same (at least the ones I've come across).

There are some exceptions like Ruby & Lisp which treats 0 as true,  AFAIK.

If you're wondering why 0s & 1s ... well I don't have a definite answer myself as it's one of those topics that will divide opinion.
IMHO I side with the view that it originates from Electronics & Boolean Algebra.
But it certainly sounds like a question for Quora :)

  • Like 3

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By CareerTeam GmbH
      Hi there,
      We are an executive search agency based in Germany looking for a freelancer (2-5 days per week) supporting us with the development and design of our websites. The position will be located in Hamburg, Germany and it would be great if you are on short call. German language knowledge is mandatory. 
      You can reach me via email jobs@careerteam.de.
      Thank you!
      Regards
      Annemie
    • By louisstephens
      I was really unsure of how to actually title this post, so I do apologize (if someone has a better idea, I will gladly edit it). I am using the profields: pagetable field to allow people to create their own "content" (copy, image, button, etc etc) and rearrange it. I also included a field called "column_size" using the RangeSlider set to (1-12).
      I guess I'll clarify a bit more on this. I am using flexbox where the "row" is <section></section> and the columns are <div class="column"></div> have given the "columns"  flex: 1 1 0; so no matter how many columns you have, the columns will automatically adjust for new content. Where my confusion is coming in: If a user has set up 3 copy items (with 12, 5, 7 respectfully for the column_size), how do I actually output this in my template? I was going to use a switch statement to handle the various items which I thought made it quite easy, but with closing sections and columns I have confused myself as I assume I need an if statement to check if the column size is > 12, or = 12 to determine the actual closing/opening of sections. I apologize if I have not made this very clear. I am a bit unsure how to word this let alone to go about this. 
      Im very appreciative of for any insight into this.
       
       
    • By louisstephens
      So I have been hard at work creating url segments for a template (api) and everything is working swimmingly in creating a simple end point for svelte.js. I have however, run into a few questions that I can wrap my head around.
      In my api template I have:
      if($input->urlSegment1 === 'clients') { header('Content-Type: application/json'); $clients = $pages->find("template=clients"); $client_array = array(); foreach ($clients as $client) { $id = $client->id; $title = $client->title; $url = $client->url; $clientName = $client->client_name; $clientColor = $client->client_color->value; $assigned = $client->assigned_to->user_full_name; $client_array[] = array( 'id' => $id, 'code' => $title, 'name' => $clientName, 'associated_users' => $assigned, 'url' => $url ); } $client_json = json_encode($client_array, true); echo $client_json; } The output json from this is:
      [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was curious is it possible to add in "clients" before this output json so it would appear as 
      clients: [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was not really sure of how to tackle this in my php code, and have spent more time than I care to admit trying to figure it out. Another question I have is that "associated_users" is returning null, which in this instance is correct. It is a multi page field that is set to pull a custom name field from the users template, ie "Louis Stephens" would be associated with the first page. I understand that I need to use a foreach to get the correct data, but I was really unsure of how to place this inside an array, or update the array with the new data. Any help with any of this would greatly be appreciated.
    • By Mithlesh
      Changing it to null
×
×
  • Create New...