Page 1 of 3 123 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/add.php

    Code:
    <?php
    
    $M = 20;
    $N = 50;
    echo $M + $N;

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

    Default

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

    Code:
    <?php
    
    $M = 20;
    $N = 50;
    echo $M + $N;
    Quote Originally Posted by admin View Post
    It is always better to use descriptive name for variables, so you what what is stored in a variable. For example, to store mark, we use variable $mark
    It is ok, but better use descriptive names for variables. One letter variable names can be confusing in most cases.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  3. #3
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    I got an error while adding break in http://php.flashwebhost.com/annie/company.php

    Code:
    Parse error:  syntax error, unexpected '>' in /home/fwhphp/public_html/annie/company.php on line 3 
    Code:
     <?php
    $companyname = 'Relience';
    echo $companyname <br> ; 
    $companyname = 'Whirpool'; 
    echo $companyname ;

  4. #4
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    I got an error http://php.flashwebhost.com/sherly/name.php

    Code:
    Parse error:  syntax error, unexpected '' ;' (T_ENCAPSED_AND_WHITESPACE), expecting ',' or ';' in /home/fwhphp/public_html/sherly/name.php on line 7
    Code:
    <?php
    $my_string = 'Hi Meera. My anme is Neena';
    $my_number = 8;
    $my_letter = N;
    echo $my_string;
    echo $my_number;
    echo $my_letter' ;
    Last edited by sherlyk; 05-09-2014 at 07:08 AM.

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

    Default

    Quote Originally Posted by sherlyk View Post
    I got an error http://php.flashwebhost.com/sherly/name.php

    Code:
    <?php
    $my_string = 'Hi Meera. My anme is Neena';
    $my_number = 8;
    $my_letter = N;
    echo $my_string;
    echo $my_number;
    echo $my_letter' ;
    Good, more friendly code with some string.

    Code:
    <?php
    
    $my_string = 'Hi Meera. My anme is Neena';
    $my_number = 8;
    $my_letter = N;
    
    echo '<h1>About Me</h1>';
    echo $my_string;
    echo '<br>';
    echo 'My number is ' . $my_number;
    echo '<br>';
    echo 'My Name start with ' . $my_letter' ;
    echo '<br>';
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

    Quote Originally Posted by annie View Post
    I got an error while adding break in http://php.flashwebhost.com/annie/company.php

    Code:
    Parse error:  syntax error, unexpected '>' in /home/fwhphp/public_html/annie/company.php on line 3 
    Code:
     <?php
    $companyname = 'Relience';
    echo $companyname <br> ; 
    $companyname = 'Whirpool'; 
    echo $companyname ;
    Problem is on line number 3.

    Code:
    echo $companyname <br> ;
    To fix, replace that line with

    Code:
    echo $companyname .  '<br>' ;
    $companyname is a variable. We use DOT to join it with string <br>. String should start with a quote, end with a quote.

    As for variable name, proper naming convention is $companyName, because it is two words, and easy to read.


    Fixed code

    Code:
    <?php
    
    $companyName = 'Relience';
    
    echo $companyname .  '<br>'; 
    
    // here we store another text to same variable $companyName
    $companyName = 'Whirpool'; 
    
    echo $companyname ;
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  7. #7
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

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

    Code:
     
    <?php
    $name = 'George';
    $Rank = 1;
    $Mark = 800;
    $School = 'Raja Giri Public School';
    echo $name . ' from ' . $School . ' got Rank ' . $Rank . ' He have ' . $Mark . 'Marks';

  8. #8
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    I didn't get the correct answer. http://php.flashwebhost.com/sherly/variable.php

    Code:
    <?php
    
    $variable12 = $variable7 + $variable5;
    echo $variable7;
    echo " + ";
    echo $variable5;
    echo " = ";
    echo $variable12;

  9. #9
    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. ")

  10. #10
    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.

Page 1 of 3 123 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
  •