कंप्यूटर प्रोग्रामिंग/स्ट्रिंग्स/पीएचपी

strings.php सम्पादन

<?php

// This program splits a given comma-separated name into first and last name
// components and then displays the name.

function get_name()
{
    do
    {
        echo "Enter name (last, first):\n";
        $name = readline();
        $index = strpos($name, ",");
    } while ($index === FALSE);
    
    return $name;
}

function get_last($name)
{
    $index = strpos($name, ",");
    if ($index === FALSE)
    {
        $last = "";
    }
    else
    {
        $last = substr($name, 0, $index);
    }
    
    return $last;
}

function get_first($name)
{
    $index = strpos($name, ",");
    if($index === FALSE)
    {
        $first = "";
    }
    else
    {
        $first = substr($name, $index + 1);
        $first = trim($first);
    }

    return $first;
}

function display_name($first, $last)
{
    echo "Hello " . $first . " " . $last . "!\n";
}

function main()
{
    $name = get_name();
    $last = get_last($name);
    $first = get_first($name);
    display_name($first, $last);
}

main();

?>

कोशिश करो सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें सम्पादन