What do these characters do?

Discussion in 'PHP' started by dtm32236, Feb 26, 2009.

  1. #1
    Hi,

    I keep on seeing these characters in PHP, but I have no idea what they do. I also can't look them up on Google because I don't know what they're called.

    Anyway, the 2 expressions (or whatever they are) are:

    %s
    example: sprintf("INSERT INTO products (`name`, `description`, `user_id`) VALUES ('%s', '%s', %d)", mysql_real_escape_string($product_name, $link), mysql_real_escape_string($product_description, $link),

    =>
    example: foreach($_GET as $k=>$v)

    Will anyone fill me in on what these are, what they do or what they're called?

    Thanks a lot!
     
    dtm32236, Feb 26, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    shallowink, Feb 26, 2009 IP
  3. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #3
    sprintf is a function that formats a string using other variables. the first parameter is the format and the other parameters are all the variables. the %s and %d (there are more) are placeholders to a specific type of variable.
    for example, if you put %s, the variable is treated as string. %d is treated as integer.
    so
    sprintf("my name is %s and i'm %d years old", 'yo' . 'av', 10 + 15) will print "my name is yoav and i'm 25 years old"

    => is used to associate a key to a value in an array
    PHP's arrays are all associative. in shallowink's example, '1' is associated to "Apples" and '2' to "Oranges".
     
    yoavmatchulsky, Feb 26, 2009 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    That is the type specifier for functions like sprintf which format a string.

    - ads2help
     
    ads2help, Feb 26, 2009 IP
  5. dtm32236

    dtm32236 Guest

    Messages:
    320
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Perfect - Thanks a lot everyone. This helped a lot.
     
    dtm32236, Feb 27, 2009 IP
  6. dtm32236

    dtm32236 Guest

    Messages:
    320
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hey guys,

    One more... can someone post a page that will explain this:

    ->

    example: $fields[$this->name.'.'.$field] = $value;

    Thanks again!
    -Dan
     
    dtm32236, Mar 2, 2009 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The dot is the concatenation operator. It smushes two or more strings together into one. In this case there are three strings lined up for besmushal: $this->name, a period, and $field.

    So if $this->name == 'tomato' and $field == 'sandwich', then it's effectively doing:

    $fields['tomato.sandwich'] = $value;
     
    SmallPotatoes, Mar 2, 2009 IP
  8. dtm32236

    dtm32236 Guest

    Messages:
    320
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks SmallPotatoes.

    I'm more concerned about this part:

    $this->name

    I'm not sure what the -> does. It looks like it returns array values or something... ?
     
    dtm32236, Mar 3, 2009 IP
  9. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #9
    $this refers to the current object, and -> is how you reference an object property. If this terminology doesn't make sense, you'd be best off reading about PHP object-oriented programming at http://www.php.net/manual/en/language.oop5.basic.php
     
    SmallPotatoes, Mar 3, 2009 IP
  10. dtm32236

    dtm32236 Guest

    Messages:
    320
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Perfect. I'll check that out.

    Thanks again, SmallPotatoes.
     
    dtm32236, Mar 3, 2009 IP