Quotes help make search much faster. Example: "Practice Makes Perfect"

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Saturday, April 27, 2013

Codecademy Sample Solution: Challenge Time


<!DOCTYPE html>
<html>
    <head>
 <title> Challenge Time! </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <?php
          // Your code here
            class Cat{
                public $isAlive = true;
                public $numLegs = 4;
                public $name;
               
                public function __construct($name){
                    $this->name = $name;
                }
               
                public function meow(){
                    return "Meow meow";
                }
            }
           
            $cat1 = new Cat('CodeCat');
            echo $cat1->meow();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Putting It All Together, Part II


<!DOCTYPE html>
<html>
<head>
 <title> Practice makes perfect! </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Dog{
                public $numLegs = 4;
                public $name;
               
                public function __construct($name){
                    $this->name = $name;
                }
               
                public function bark(){
                    return "Woof!";
                }
               
                public function greet(){
                    return "My name is $this->name.";
                }
            }
           
            $dog1 = new Dog('Barker');
            $dog2 = new Dog('Amigo');
            echo $dog1->bark();
            echo $dog2->greet();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Putting It All Together, Part I


<!DOCTYPE html>
<html>
<head>
 <title> Practice makes perfect! </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Dog{
                public $numLegs = 4;
                public $name;
               
                public function __construct($name){
                    $this->name = $name;
                }
               
                public function bark(){
                   
                }
               
                public function greet(){
                   
                }
            }
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: A Method to the Madness


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
                public $isAlive = true;
                public $firstname;
                public $lastname;
                public $age;
               
                public function __construct($firstname, $lastname, $age){
                    $this->firstname = $firstname;
                    $this->lastname = $lastname;
                    $this->age = $age;
                }
               
                public function greet(){
                    return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
                }
            }
            $teacher = new Person('boring', '12345', 12345);
            echo $teacher->greet();
            $student = new Person('Anony', 'mous', 100);
            echo $student->greet();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Property Panic (2)


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
                public $isAlive = true;
                public $firstname;
                public $lastname;
                public $age;
               
                public function __construct($firstname, $lastname, $age){
                    $this->firstname = $firstname;
                    $this->lastname = $lastname;
                    $this->age = $age;
                }
            }
            $teacher = new Person('boring', '12345', 12345);
            $student = new Person('Anony', 'mous', 100);
            echo $student->age;
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Property Panic (1)


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
                public $isAlive = true;
                public $firstname;
                public $lastname;
                public $age;
            }
            $teacher = new Person();
            echo "$teacher->isAlive";
            $student = new Person();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Building Your First Class


<!DOCTYPE html>
<html>
<head>
 <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
        <!-- Your code here -->
        <?php
            class Person{
               
            }
            $teacher = new Person();
            $student = new Person();
        ?>
      </p>
    </body>
</html>

Friday, April 26, 2013

Codecademy Sample Solution: Putting it All Together


<html>
  <head>
    <title>I am the King of Arrays!</title>
  </head>
  <body>
    <p>
      <?php
      // On the line below, create your own associative array:
      $myArray = array('feet'=>'shoes', 'hands'=>'gloves', 'head'=>'hat');

      // On the line below, output one of the values to the page:
      print $myArray['head']."<br/>";
         
      // On the line below, loop through the array and output
      // *all* of the values to the page:
      foreach($myArray as $key => $value){
          print $key .'=&gt;'. $value."<br/>";
      }
   
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Multidimensional Arrays


<html>
  <head>
    <title>Blackjack!</title>
  </head>
  <body>
    <p>
      <?php
        $deck = array(array('2 of Diamonds', 2),
                      array('5 of Diamonds', 5),
                      array('7 of Diamonds', 7),
                      array('8 of Clubs', 8));
       
      // Imagine the first chosen card was the 7 of Diamonds.
      // This is how we would show the user what they have:
      echo 'You have the ' . $deck[3][0] . '!';
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Iterating Over Associative Arrays


<html>
  <head>
    <title>Iteration Nation</title>
  </head>
  <body>
    <p>
      <?php  
        $food = array('pizza', 'salad', 'burger');
        $salad = array('lettuce' => 'with',
                   'tomato' => 'without',
                   'onions' => 'with');
   
      // Looping through an array using "for".
      // First, let's get the length of the array!
      $length = count($food);
   
      // Remember, arrays in PHP are zero-based:
      for ($i = 0; $i < $length; $i++) {
        echo $food[$i] . '<br />';
      }
   
      echo '<br /><br />I want my salad:<br />';
   
      // Loop through an associative array using "foreach":
      foreach ($salad as $ingredient=>$include) {
        echo $include . ' ' . $ingredient . '<br />';
      }
   
      echo '<br /><br />';
   
      // Create your own array here and loop
      // through it using foreach!
      $gadget = array('programming' => 'iMac', 'calls' => 'iPhone', 'ebooks' => 'iPad');
      foreach ($gadget as $key => $value){
        echo "I use the ".$value." for ".$key.".<br/>";
      }

      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Accessing Associative Arrays


<html>
  <head>
    <title>Accessing Associative Arrays</title>
  </head>
  <body>
    <p>
      <?php
        // This is an array using integers as the indices...
        $myArray = array(2012, 'blue', 5, 'BMW');

        // ...and this is an associative array:
        $myAssocArray = array('year' => 2012,
                        'colour' => 'blue',
                        'doors' => 5,
                        'make' => 'BMW');
           
        // This code will output "blue".
        echo $myArray[1];
        echo '<br />';
           
        // Add your code here!
        echo "My car was purchased in $myArray[0] and has ". $myAssocArray['doors'] ." doors.";
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Using Arrays as Maps


<html>
  <head>
    <title>Making the Connection</title>
  </head>
  <body>
    <p>
      <?php
        // This is an array using integers as the indices.
        // Add 'BMW' as the last element in the array!
        $car = array(2012, 'blue', 5, 'BMW');

        // This is an associative array.
        // Add the make => 'BMW' key/value pair!
        $assocCar = array('year' => 2012,
                   'colour' => 'blue',
                   'doors' => 5,
                   'make' => 'BMW');
           
        // This code should output "BMW"...
        echo $car[3];
        echo '<br />';
           
        // ...and so should this!
        echo $assocCar['make'];
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Review of Arrays


<html>
  <head>
    <title>Array Review</title>
  </head>
  <body>
    <p>
      <?php
        $fruits = array("bananas", "apples", "pears");              /* Your code here! */
        echo 'I love eating ' . $fruits[1]/* Your code here! */ . ' too!';
      ?>
    </p>
  </body>
</html>

Codecademy Sample Solution: Practice Makes Perfect


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
            function aboutMe($name, $age){
                echo "Hello! My name is $name, and I am $age years old.";
            }
           
            aboutMe("Anonymous",100);
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Parameters and Arguments


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
            function greetings($name){
                echo "Greetings, $name!";
            }
           
            greetings("Anonymous");
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: The Return Keyword


<html>
<head>
<title></title>
</head>
<body>
      <?php
           
        function returnName(){
            return "Anonymous";
        }
       
        print returnName();
      ?>
    </body>
</html>

Codecademy Sample Solution: Calling Your Function


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
        // Write your function below!
       
        function displayName(){
            echo "Anonymous";
        }
       
        displayName();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Your First Function


<html>
<head>
<title></title>
</head>
<body>
      <p>
        <?php
        // Write your function below!
       
        function displayName(){
            echo "Anonymous";
        }
       
        displayName();
        ?>
      </p>
    </body>
</html>

Codecademy Sample Solution: Function Refresher


<html>
    <head>
<title></title>
</head>
<body>
      <p>
        <?php
            echo strlen("Anonymous");
        ?>
      </p>
    </body>
</html>

Thursday, April 25, 2013

Codecademy Sample Solution: Show What You Know!


<html>
    <p>
<?php
// Create an array and push on the names
    // of your closest family and friends
    $somearray = array();
    array_push($somearray, "Garfield");
    array_push($somearray, "Odie");
    array_push($somearray, "John");
// Sort the list
    sort($somearray);
// Randomly select a winner!
    $arraylength = count($somearray);
    $winner = $somearray[rand(0,$arraylength-1)];
// Print the winner's name in ALL CAPS
    print strtoupper($winner);
?>
</p>
</html>

This is an example of scrolling text using Javascript.

Popular Posts