Getting into PHP

Discussion in 'Web-development' started by FakeJR, Jan 17, 2015.

  1. FakeJR

    FakeJR Coffee?

    Joined:
    Nov 25, 2014
    Messages:
    100
    Likes Received:
    24
    Best Answers:
    0
    This is my first attempt to create just something in PHP.

    <?php

    $names = array('Chris' , 'Tom' , 'Bob');
    $yrs = array('Chris'=>18 , 'Tom'=>13 , 'Bob'=>15);

    echo 'My name is ' . $names[1] . ' and im ' . $yrs[Tom] . ' yrs old.';

    ?>

    But I got this error: Notice: Use of undefined constant Tom - assumed 'Tom' in C:\xampp\htdocs\---\test.php on line 6

    Note: I know this is really easy to make but i've never done it before.

    Also: Could you link some good tutorials in the comments if you do comment would really appreciate that. :)
     
  2. Breed

    Breed Active Member

    Joined:
    Jan 7, 2015
    Messages:
    517
    Likes Received:
    121
    Best Answers:
    0
    should be
    PHP:
    1. echo 'My name is ' . $names[1] . ' and im ' . $yrs['Tom'] . ' yrs old.';
     
    FakeJR likes this.
  3. FakeJR

    FakeJR Coffee?

    Joined:
    Nov 25, 2014
    Messages:
    100
    Likes Received:
    24
    Best Answers:
    0
    Oh, sh!t I forgot those.. Thanks! haha.
     
  4. staipi

    staipi HTML5 Game Designer

    Joined:
    Aug 5, 2009
    Messages:
    27
    Likes Received:
    9
    Best Answers:
    0
    or
    Code (Text):
    1. <?php
    2.  
    3. $names = array('Chris' , 'Tom' , 'Bob');
    4. $yrs = array('Chris'=>18 , 'Tom'=>13 , 'Bob'=>15);
    5.  
    6.  
    7. echo 'My name is ' . $names[1] . ' and im ' . $yrs[$names[1]] . ' yrs old.';
    8.  
    9. ?>
     
    Breed likes this.
  5. EvulMastah

    EvulMastah ๏̯͡๏﴿ Premium User

    Joined:
    Aug 19, 2007
    Messages:
    4,827
    Likes Received:
    204
    Best Answers:
    0
    Code (Text):
    1. <?php
    2.     $people = [ 'Chris' => 18, 'Tom' => 13, 'Bob' => 15 ];
    3.     echo 'My name is '.array_keys($people)[1].' and im '.array_values($people)[1].' yrs old.';
    4. ?>
     
  6. averatec

    averatec Well-Known Member

    Joined:
    Jun 1, 2007
    Messages:
    2,215
    Likes Received:
    142
    Best Answers:
    1
    PHP:
    1.  
    2. <?php
    3. $people = [
    4.     [ 'name' => 'Chris',  'age' => 18 ],
    5.     [ 'name' => 'Tom',    'age' => 17 ],
    6.     [ 'name' => 'Bob',    'age' => 15 ]
    7. ];
    8. echo sprintf('My name is %s and I\'m %d yrs old.', $people[1]['name'], $people[1]['age']);
    9.  
     
  7. byllan1337

    byllan1337 I'm back!

    Joined:
    Nov 4, 2013
    Messages:
    88
    Likes Received:
    3
    Best Answers:
    0
  8. averatec

    averatec Well-Known Member

    Joined:
    Jun 1, 2007
    Messages:
    2,215
    Likes Received:
    142
    Best Answers:
    1
  9. FakeJR

    FakeJR Coffee?

    Joined:
    Nov 25, 2014
    Messages:
    100
    Likes Received:
    24
    Best Answers:
    0
    I know theres free tutorials, and I wasn't gonna buy a "book" to learn this lol.
    Thanks anyway.
     
  10. Breed

    Breed Active Member

    Joined:
    Jan 7, 2015
    Messages:
    517
    Likes Received:
    121
    Best Answers:
    0
    PHP:
    1. <?php
    2.     class People{
    3.  
    4.         protected $name;
    5.         protected $age;
    6.      
    7.         function __construct($name, $age) {
    8.             $this->name = $name;
    9.             $this->age = $age;
    10.         }
    11.     }
    12.  
    13.     class Person extends People{
    14.  
    15.         function __construct($name, $age) {
    16.             parent::__construct($name, $age);
    17.         }
    18.      
    19.         public function getPerson(){
    20.             echo sprintf('My name is %s and I\'m %d yrs old.', $this->name, $this->age);
    21.         }
    22.     }
    23.  
    24.     $tom = new Person('Tom', 17);
    25.  
    26.     $tom->getPerson();
    27. ?>
     
    Zerak and FakeJR like this.
  11. averatec

    averatec Well-Known Member

    Joined:
    Jun 1, 2007
    Messages:
    2,215
    Likes Received:
    142
    Best Answers:
    1
    PHP:
    1.  
    2. <?php
    3. class Person {
    4.     protected $name;
    5.     protected $age;
    6.  
    7.     public function __construct($name, $age) {
    8.         $this->
    9.             setName($name)
    10.             setAge($age)
    11.         ;
    12.     }
    13.  
    14.     public function getName() {
    15.         return $this->name;
    16.     }
    17.  
    18.     public function setName($name) {
    19.         $this->name = $name;
    20.         return $this;
    21.     }
    22.  
    23.     public function getAge() {
    24.         return $this->age;
    25.     }
    26.  
    27.     public function setAge($age) {
    28.         $this->age = $age;
    29.         return $this;
    30.     }
    31.  
    32.     public function __toString() {
    33.         return sprintf('My name is %s and I\'m %d yrs old.', $this->getName(), $this->getAge());
    34.     }
    35. }
    36.  
    37. $Chris = new Person('Chris', 24);
    38.  
    39. echo $Chris;
    @Chris
    corrected :D
     
    Last edited: Jan 28, 2015
    Chris and Breed like this.
  12. Chris

    Chris Inactive

    Joined:
    Aug 11, 2008
    Messages:
    2,628
    Likes Received:
    235
    Best Answers:
    2
    I'm 24 :(
     
    averatec likes this.
  13. Breed

    Breed Active Member

    Joined:
    Jan 7, 2015
    Messages:
    517
    Likes Received:
    121
    Best Answers:
    0
    Your lucky I have to leave the house haha
     
    averatec likes this.
  14. Breed

    Breed Active Member

    Joined:
    Jan 7, 2015
    Messages:
    517
    Likes Received:
    121
    Best Answers:
    0
    Just quick question why make age & name protected if there is no intention of inheritance you could have just made them private?

    I made them protected because there is 2 classes a parent (People) and child (Person)

    Lets simplify your class so that there is only 1 method per property to set and get it.
    PHP:
    1. <?php
    2. class Person {
    3.     private $name;
    4.     private $age;
    5.     public function __construct($name, $age) {
    6.         $this->Name($name);
    7.         $this->Age($age);
    8.     }
    9.  
    10.         public function Name($name=null) {
    11.         $this->name = isset($name) ? $name : $this->name;
    12.         return $this->name;
    13.         }
    14.         public function Age($age=null) {
    15.             $this->age = isset($age) ? $age : $this->age;
    16.             return $this->age;
    17.         }
    18.     public function __toString() {
    19.         return 'My name is '.$this->Name().' and I\'m '.$this->Age().' yrs old.';
    20.     }
    21. }
    22.  
    23. $Chris = new Person('Chris', 24);
    24.  
    25. echo $Chris; // My name is Chris and I'm 24 yrs old.
    26. ?>
    The name & age method do not need to be public to be used in __toString(), they can be private or protected as well.
     
    Last edited: Feb 1, 2015
  15. averatec

    averatec Well-Known Member

    Joined:
    Jun 1, 2007
    Messages:
    2,215
    Likes Received:
    142
    Best Answers:
    1
    why did I make them protected? well it's simple only library funcionality should be private, it's good habbit
    also it's good to take just one naming convention,
    what's more everything should be as simple as it's possible so your getters/setters don't follow this rule
    anyway I'm not saying your code isn't good
     
    Last edited: Feb 1, 2015
    Codex NG likes this.
  16. Breed

    Breed Active Member

    Joined:
    Jan 7, 2015
    Messages:
    517
    Likes Received:
    121
    Best Answers:
    0
    This is your class's method output of setName($name) when I use print_r
    PHP:
    1.  
    2. Person Object
    3. (
    4.     [name:protected] => Chris
    5.     [age:protected] => 24
    6. )
    7.  
    This is my modified version of your class and its method Name() when I use print_r
    PHP:
    1.  
    2. Chris
    3.  
    Which is more secure?
     
    Last edited: Feb 1, 2015
  17. averatec

    averatec Well-Known Member

    Joined:
    Jun 1, 2007
    Messages:
    2,215
    Likes Received:
    142
    Best Answers:
    1
    well... It's called method chaining(read about Fluent Interface), you don't know it?
     
  18. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    990
    Likes Received:
    110
    Best Answers:
    0
    books > anything.
    Books are resourceful, plus they tends to induce yourself to develop your own ideas on ways of approaching things instead of going on what people says.
    I have learned a lot on internet's tutorials, but I do not change my piles of books for nothing.
     
    Codex NG and StreamSide like this.

Share This Page

Loading...