Page 1 of 2 12 LastLast
Results 1 to 10 of 50

Thread: Day 2 - Variables - Become PHP Expert in 30 days

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/age.php

    Code:
    <?php
    
    $name = 'Joseph';
    $yearBorn = 1975;
    $currentYear = 2014;
    $age = $currentYear - $yearBorn;
    
    print ("$name is $age years old. ")

  2. #2
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/age.php

    Code:
    <?php
    
    $name = 'Joseph';
    $yearBorn = 1975;
    $currentYear = 2014;
    $age = $currentYear - $yearBorn;
    
    print ("$name is $age years old. ")
    Semicolon at end of print statement missing.

    print function same as echo, but use echo, forget print. echo is more popular, also there are more ways to print, some complicated, but is not required for most programming project.

    Code:
    print ("$name is $age years old. ")
    Proper usage

    Code:
    echo $name . ' is ' .  $age . ' years old. ';
    Or

    Code:
    echo "$name  is  $age years old. ";
    Advantage of using single quote is PHP know it is just string, so it will be faster. If using double quote, PHP compilter need to look for variable inside the string and convert it to its value, that need more processing time (not much, still do that way as a convention whenever possible).
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  3. #3
    Join Date
    Oct 2003
    Location
    Kochi, Kerala, India
    Posts
    21,389

    Default

    http://php.flashwebhost.com/vineesh/...ariables_2.php

    I was facing difficulty to insert a full stop after a sentence. But I managed to make it work. My code is

    Code:
    <?php
    
    $staffName = 'Ramesh';
    $companyName = 'HOSTONNET';
    
    $staffName2 = 'Suresh';
    
    $staffName3 = 'Tom';
    
    echo $staffName . ' is working in ' . $companyName;  echo '.'; echo ' But '; echo $staffName2; echo ' not.' ;
    
    echo '<br>';
    
    echo $staffName3 . ' also working in ' . $companyName . 'for the past 10 years'; echo '.';
    Is there any issue with this ?

  4. #4
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 2 PHP PROGRAMMING

    http://php.flashwebhost.com/tom/day_2_variables_1.php

    Code:
    <?php
    
    
    $MovieName = '<b>Mr. Fraud</b>';
    
    
    echo $MovieName ;

  5. #5
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 2 PROGRAMMING

    http://php.flashwebhost.com/tom/day_2_variables_2.php

    Code:
    <?php
    
    
    $MovieName = '<b>Mr. Fraud</b>';
    
    
    echo 'Mohanlal\'s Upcoming Movie '  .  $MovieName;

  6. #6
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 2 PROGRAMMING

    http://php.flashwebhost.com/tom/day_2_variables_3.php

    Code:
    <?php
    
    
    $MovieName = 'Mr. Fraud' ;
    $Starring = 'Mohanlal, Siddique, Dev Gill, Pallavi, Mia, Manjari' ;
    $Director = 'B. Unnikrishnan' ;
    
    
    echo $MovieName . ' is a malayalam movie, ' . $Director . ' is the Director. ' . $Starring . ' in the lead roles. ' ;

  7. #7
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by image View Post
    DAY 2 PROGRAMMING

    http://php.flashwebhost.com/tom/day_2_variables_2.php

    Code:
    <?php
    
    
    $MovieName = '<b>Mr. Fraud</b>';
    
    
    echo 'Mohanlal\'s Upcoming Movie '  .  $MovieName;
    Variable names always start with smaller case letter.

    Code:
    <?php
    
    
    $movieName = '<b>Mr. Fraud</b>';
    
    
    echo 'Mohanlal\'s Upcoming Movie '  .  $movieName;
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  8. #8
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 2 PHP PROGRAMMING

    http://php.flashwebhost.com/tom/day_2_variables_4.php

    Code:
    <?php
    
    
    $numOfBoys = '20' ;
    $numOfGirls = '25' ;
    
    
    echo 'Total number of students = ' . ($numOfBoys+$numOfGirls) ;
    Last edited by image; 05-09-2014 at 10:48 AM.

  9. #9
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by image View Post
    DAY 2 PHP PROGRAMMING

    http://php.flashwebhost.com/tom/day_2_variables_4.php

    Code:
    
    <?php
    
    
    $numboys = '20' ;
    $numgirls = '25' ;
    
    
    echo 'Total number of students = ' . ($numboys+numgirls) ;

    This program have syntax error on line

    Code:
    echo 'Total number of students = ' . ($numboys+numgirls) ;
    $ missing for numgirls.

    Proper usage.

    Code:
    <?php
    
    $numBoys = 20;
    $numGirls = 25;
    
    echo 'Total number of students = ' . ($numBoys + $numGirls) ;
    Only strings values need to be enclosed within quotes (' or ").

    Variable names, if two words, Capitalize first letter of 2nd, 3rd, etc.. word.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  10. #10
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by vineesh View Post
    http://php.flashwebhost.com/vineesh/...ariables_2.php

    I was facing difficulty to insert a full stop after a sentence. But I managed to make it work. My code is

    Code:
    <?php
    
    $staffName = 'Ramesh';
    $companyName = 'HOSTONNET';
    
    $staffName2 = 'Suresh';
    
    $staffName3 = 'Tom';
    
    echo $staffName . ' is working in ' . $companyName;  echo '.'; echo ' But '; echo $staffName2; echo ' not.' ;
    
    echo '<br>';
    
    echo $staffName3 . ' also working in ' . $companyName . 'for the past 10 years'; echo '.';
    Is there any issue with this ?

    Try this

    Code:
    <?php
    
    $staffName = 'Ramesh';
    $companyName = 'HOSTONNET';
    
    $staffName2 = 'Suresh';
    
    $staffName3 = 'Tom';
    
    echo $staffName . ' is working in ' . $companyName .  '.' .  ' But ' .  $staffName2 . ' not.' ;
    
    echo '<br>';
    
    echo $staffName3 . ' also working in ' . $companyName . ' for the past 10 years.';
    You can also replace

    Code:
    echo $staffName . ' is working in ' . $companyName .  '.' .  ' But ' . $staffName2 . ' not.' ;
    
    echo '<br>';
    
    echo $staffName3 . ' also working in ' . $companyName . ' for the past 10 years.';
    With

    Code:
    echo '<p>' . $staffName . ' is working in ' . $companyName .  '.' .  ' But ' .  $staffName2 . ' not.</p>' ;
    
    echo '<p>' . $staffName3 . ' also working in ' . $companyName . ' for the past 10 years.</p>';
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •