Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Dev sOs Net

Dev sOs Net Logo Dev sOs Net Logo

Dev sOs Net Navigation

  • Home
  • About Us
  • Dev Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About Us
  • Dev Blog
  • Contact Us
Home/ Questions/Q 68
Next
Answered

Dev sOs Net Latest Questions

Dev
  • 2
  • 2
DevTeacher
Asked: April 18, 20182018-04-18T10:07:14+00:00 2018-04-18T10:07:14+00:00In: 1. WordPress Hosting, 1.2. PHP Nutshell, 2. Linux Hosting, 2.2. PHP Nutshell, Knowledge Base

PHP Cookbook – Recipe 1.1 Introduction

  • 2
  • 2

Problem

Strings in PHP are a sequence of characters, such as “We hold these
truths to be self evident,” or “Once upon a time,” or even
“111211211.” When you read data from a file or output it to a web
browser, your data is represented as strings?

2. PHP AuthorAdam TrachtenbergDavid Sklar
  • 1 1 Answer
  • 66 Views
  • 1 Follower
  • 2
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse


1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. David Sklar, Adam Trachtenberg
    Best Answer
    David Sklar, Adam Trachtenberg
    2018-04-18T10:07:49+00:00Added an answer on April 18, 2018 at 10:07 am
    This answer was edited.

    Solution

    Individual characters in strings can be referenced with array subscript
    style notation, as in C. The first character in the string is at index 0.
    For example:
    $neighbor = ‘Hilda’;
    print $neighbor[3];
    d
    However, PHP strings differ from C strings in that they are binary-safe
    (i.e., they can contain null bytes) and can grow and shrink on
    demand. Their size is limited only by the amount of memory that is
    available.
    You can initialize strings three ways, similar in form and behavior to
    Perl and the Unix shell: with single quotes, with double quotes, and
    with the “here document” (heredoc) format. With single-quoted
    strings, the only special characters you need to escape inside a string
    are backslash and the single quote itself:
    print ‘I have gone to the store.’;
    print ‘I’ve gone to the store.’;
    print ‘Would you pay $1.75 for 8 ounces of tap
    water?’;
    print ‘In double-quoted strings, newline is
    represented by \n’;
    I have gone to the store.
    I’ve gone to the store.
    Would you pay $1.75 for 8 ounces of tap water?
    In double-quoted strings, newline is represented by
    \n
    Because PHP doesn’t check for variable interpolation or almost any
    escape sequences in single-quoted strings, defining strings this way
    is straightforward and fast.
    Double-quoted strings don’t recognize escaped single quotes, but
    they do recognize interpolated variables and the escape sequences
    shown in Table 1-1.
    Table 1-1. Double-quoted string escape sequences
    Escape sequence Character
    \n Newline (ASCII 10)
    \r Carriage return (ASCII 13)
    \t Tab (ASCII 9)
    \ Backslash
    \$ Dollar sign
    \” Double quotes
    \{ Left brace
    \} Right brace
    \[ Left bracket
    \] Right bracket
    \0 through \777 Octal value
    \x0 through \xFF Hex value
    For example:
    print “I’ve gone to the store.”;
    print “The sauce cost \$10.25.”;
    $cost = ‘$10.25’;
    print “The sauce cost $cost.”;
    print “The sauce cost \$\061\060.\x32\x35.”;
    I’ve gone to the store.
    The sauce cost $10.25.
    The sauce cost $10.25.
    The sauce cost $10.25.
    The last line of code prints the price of sauce correctly because the
    character 1 is ASCII code 49 decimal and 061 octal. Character 0 is
    ASCII 48 decimal and 060 octal; 2 is ASCII 50 decimal and 32 hex;
    and 5 is ASCII 53 decimal and 35 hex.
    Heredoc-specified strings recognize all the interpolations and escapes
    of double- quoted strings, but they don’t require double quotes to be
    escaped. Heredocs start with <<< and a token. That token (with no
    leading or trailing whitespace), followed by a semicolon to end the
    statement (if necessary), ends the heredoc. For example:
    print <<< END
    It’s funny when signs say things like:
    Original “Root” Beer
    “Free” Gift
    Shoes cleaned while “you” wait
    or have other misquoted words.
    END;
    It’s funny when signs say things like:
    Original “Root” Beer
    “Free” Gift
    Shoes cleaned while “you” wait
    or have other misquoted words.
    With heredocs, newlines, spacing, and quotes are all preserved. The
    end-of-string identifier is usually all caps, by convention, and it is
    case sensitive. Thus, this is okay:
    print <<< PARSLEY
    It’s easy to grow fresh:
    Parsley
    Chives
    on your windowsill
    PARSLEY;
    So is this:
    print <<< DOGS
    If you like pets, yell out:
    DOGS AND CATS ARE GREAT!
    DOGS;
    Heredocs are useful for printing out HTML with interpolated variables:
    if ($remaining_cards > 0) {
    $url = ‘/deal.php’;
    $text = ‘Deal More Cards’;
    } else {
    $url = ‘/new-game.php’;
    $text = ‘Start a New Game’;
    }
    print <<< HTML
    There are <b>$remaining_cards</b> left.
    <p>
    <a href=”$url”>$text</a>
    HTML;
    In this case, the semicolon needs to go after the end-of-string
    delimiter, to tell PHP the statement is ended. In some cases, however,
    you shouldn’t use the semicolon:
    $a = <<< END
    Once upon a time, there was a
    END
    . ‘ boy!’;
    print $a;
    Once upon a time, there was a boy!
    In this case, the expression needs to continue on the next line, so
    you don’t use a semicolon. Note also that in order for PHP to
    recognize the end-of-string delimiter, the . string concatenation
    operator needs to go on a separate line from the end-of-string
    delimiter.

      • 2
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 10
  • Answers 10
  • Best Answers 7
  • User 1
  • Popular
  • Answers
  • Dev

    Apache Cookbook - Recipe 1.1 Installing from Red Hat Linux’s ...

    • 1 Answer
  • Dev

    Linux Cookbook - Recipe 1.1 Introduction

    • 1 Answer
  • Dev

    PHP Cookbook - Recipe 1.1 Introduction

    • 1 Answer
  • Martin Hope
    Martin Hope added an answer They might be as confused as to why you keep… April 19, 2018 at 2:07 am
  • Marko Smith
    Marko Smith added an answer I have never heard a British person EVER call a… April 19, 2018 at 2:07 am
  • Barry Carter
    Barry Carter added an answer Calling a bread roll a “biscuit” really takes the biscuit.… April 19, 2018 at 2:07 am

Related Questions

  • SQL Cookbook - Recipe 1.1 Retrieving All Rows and Columns ...

    • 1 Answer
  • Visual Basic Cookbook - Recipe 1.1 Creating a Windows Forms ...

    • 1 Answer
  • C# Cookbook - Recipe 1.1 Determining Approximate Equality Between a ...

    • 1 Answer
  • ASP.NET Cookbook - Recipe 1.1 Selecting the Right Tabular Control

    • 1 Answer
  • Windows Server Cookbook - Recipe 1.1 Introduction

    • 1 Answer

Top Members

Dev

Dev

  • 10 Questions
  • 82 Points
Teacher

Trending Tags

1. Linux Author 4. WordPress Author 5. Windows Server Author 6. ASP.NET Author 7. SQL Author Adam Trachtenberg Anthony Molinaro Carla Schroder David Sklar Geoffrey T. LeBlond Jay Hilyard John Clark Craig Ken Coar Matthew MacDonald Michael A. Kittel Paul DuBois Rich Bowen Robbie Allen Stephen Teilhet Tim Patrick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Find Solutions

Footer

Dev sOs Net

Dev sOs Net

Develop
Share Online Solution Network

sOs Net

  • About Us
  • About Team

Legal Agreement

  • Privacy Policy
  • Terms and Conditions

User Support

  • Knowledge Base
  • Contact Us

Follow Us

Copyright © Dev sOs Net. All rights reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.