Convert a Php Code into a Php Function, Please help !

Discussion in 'PHP' started by Rajnish357, Dec 25, 2011.

  1. #1
    I have this PHP code and I use this same at multiple places on the same pages, So I think it will be better if it will be a functions and I use it by calling it.

    <?php
    
    $text1Val = get_post_custom_values("thumb");
    $text2Val = get_post_custom_values("video");
    
    if($text1Val)
    {
    echo $text1Val[0]; ?>
    <?php } else if($text2Val) { ?>
    <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/<?php $values = get_post_custom_values("video"); echo $values[0]; ?>/hqdefault.jpg&amp;h=106&amp;w=196&amp;zc=1
    <?php } else { ?>
    <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/bTpM-8ZAjP4/hqdefault.jpg&amp;h=106&amp;w=196&amp;zc=1
    <?php } ?>
    PHP:
     
    Rajnish357, Dec 25, 2011 IP
  2. filegrasper

    filegrasper Active Member

    Messages:
    493
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #2
    this is the example how you can do
    
    function functionName()
    {
    code to be executed;
    }
    
    Code (markup):
    PHP function guidelines:

    Give the function a name that reflects what the function does
    The function name can start with a letter or underscore (not a number)
     
    filegrasper, Dec 25, 2011 IP
  3. Rajnish357

    Rajnish357 Well-Known Member

    Messages:
    157
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Thanks for reply filegrasper,

    Do you mean like this:
    function get_video_thumb()
    {
    <?php
    
    $text1Val = get_post_custom_values("thumb");
    $text2Val = get_post_custom_values("video");
    
    if($text1Val)
    {
    echo $text1Val[0]; ?>
    <?php } else if($text2Val) { ?>
    <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/<?php $values = get_post_custom_values("video"); echo $values[0]; ?>/hqdefault.jpg&amp;h=106&amp;w=196&amp;zc=1
    <?php } else { ?>
    <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/bTpM-8ZAjP4/hqdefault.jpg&amp;h=106&amp;w=196&amp;zc=1
    <?php } ?>
    }
    PHP:
     
    Rajnish357, Dec 25, 2011 IP
  4. filegrasper

    filegrasper Active Member

    Messages:
    493
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #4
    like this

    
    <html>
    <body>
    
    <?php
    function writeName($fname)
    {
    echo $fname . " Refsnes.<br />";
    }
    
    echo "My name is ";
    writeName("Kai Jim");
    echo "My sister's name is ";
    writeName("Hege");
    echo "My brother's name is ";
    writeName("Stale");
    ?>
    
    </body>
    </html>
    
    Code (markup):
    Output :

    My name is Kai Jim Refsnes.
    My sister's name is Hege Refsnes.
    My brother's name is Stale Refsnes.
     
    filegrasper, Dec 25, 2011 IP
  5. Rajnish357

    Rajnish357 Well-Known Member

    Messages:
    157
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Can you make change in my code and make it work please I don't know to do that..
     
    Rajnish357, Dec 25, 2011 IP
  6. thewpdev

    thewpdev Well-Known Member

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #6
    <?php

    function my_vid(){
    $text1Val = get_post_custom_values("thumb");
    $text2Val = get_post_custom_values("video");

    if($text1Val)
    {
    echo $text1Val[0]; ?>
    <?php } else if($text2Val) { ?>
    <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/<?php $values = get_post_custom_values("video"); echo $values[0]; ?>/hqdefault.jpg&amp;h=106&amp;w=196&amp;zc=1
    <?php } else { ?>
    <?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=http://img.youtube.com/vi/bTpM-8ZAjP4/hqdefault.jpg&amp;h=106&amp;w=196&amp;zc=1
    <?php
    } } ?>

    Put this function in your functions.php file and invoke/call it from anywhere inside your post loop. I am sure you're working with WordPress.
     
    thewpdev, Dec 25, 2011 IP
  7. Rajnish357

    Rajnish357 Well-Known Member

    Messages:
    157
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Thanks Cssknight for the function,

    You are absolutely right, I am on wordpress.
    Can you please also tell me how to call that code, I don't know that also.

     
    Rajnish357, Dec 25, 2011 IP
  8. ShinoRex

    ShinoRex Well-Known Member

    Messages:
    227
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    148
    #8
    Hello friend, Just call this function using the name and parameters,

    Example:

    my_vid(var1,var2);

    That is enough... Thanks
     
    ShinoRex, Dec 27, 2011 IP