Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Key Takeaways
Demonstrates PHP string manipulation using double quotes versus single quotes and tokenizing
Full Transcript
hello and welcome to the 23rd php tutorial uh in this tutorial we're going to be covering in a little bit more depth string manipulation so with that let's hop right on into it string manipulation is pretty useful for a whole lot of different applications and the first thing i really would like to show you is a scenario where you do want to use double quotes versus single quotes where it really will matter and make your life a little bit easier and really your code easier to read because at the end of the day that that matters a lot as well so sometimes it's a toss-up but let's just make an example string and we're just going to say hey there buddy is our sample string what you can do is if you let's just echo out something um if you echo out in single quotes um string and then um how are you right if you do this it's literally gonna echo out dollar sign string how are you in fact let's go ahead and put that up just to show you what's happening there so we'll refresh and it literally says string how are you but what happens if we in encase this and double quotes as you can see it's already begun to bold dollar sign string so when we save this and refresh now it actually says hey there buddy how are you and it's starting to act a little bit like we would expect it to act uh normally without using the full stop that we had done before because i mean you can also do the full stop where it's like this put that up and you get exactly the exact same string um but when you're like zooming through code and stuff and you're like maybe you're looking for you know the specific string sometimes it can be easier to both code it and read you know what's exactly happening by doing it this way so next up what we want to do is we'll leave this string here but i'm going to show you guys you know something that people typically are going to want to do is split up strings um by a specific string there's other ways you can split up strings by like word and all this kind of stuff but you can also just learn one way to split up strings and use it on every application you know that you might want to use it for so doing this is actually really simple uh with a while loop and then also you'll just need to specify the token so to do that let's just call literally a variable token say token is equal to and the way you call this is strtok string token and then within here you want to the first parameter is what you want um to apply this to so in this case we want to do this to string and then the second case is what do you want the token to be and a good way to think of it is if you ever if you're from coming from python or another's language it's really what you're going to split by so let's say you want to tokenize by the space so we'll just tokenize by a space and that's it so now if all we do is just print out a token so let's just echo token save that and come over what we get so far is just hey we don't get the full like everything split we just only get the first one so naturally we need a while loop and we're going to just loop through this so we're going to say wow token doesn't equal false now um what token is gonna can return is a false so if there's no nothing left for it to uh tokenize then it will return a false uh boolean so what's happening is what it's doing is it's like it's tokenizing by this a single time so what you want to do is just say while it doesn't equal false so you can make it dynamic here now we want to add what we want to do so let's go ahead and echo let's echo that actual token as you can see we're going to put this within double quotes so it acts as a variable and then we're going to go ahead and break double quote um and then we're going to want to rename the token so we're going to do token equals string token and then space space so what this will do is loop through and for every um well in our case it's going to be every word since we're going to split by this space and then it's going to it's gonna echo out the word split by space echo out the word split by space so uh let's go ahead and run this hopefully we don't have any typos and sure enough we get hey there buddy so that's just a way you could split by space but you of course you could split by anything else you wanted you could split by e for example so let's change this to a letter e and change this to a letter e save it come over here refresh and now you've got there buddy so anyway what you're going to split by ends up being lost much like um in python when you do a split by string variable so this is a good way to split by any method you might want now there's a couple other things that we're going to run through real quick with string manipulation and that's going to be um string length one of the things that you might want to do like say you have like a comment box and you uh want to limit you know how many comment or how many like lines of text you'll store or something like that it could be useful to know how many uh characters or even how many words are in there so the next thing you might want to do is utilize string length and that's just a str lens so let's show you that um we'll just call this string len sorry we need a dollar sign equals uh str len and then in parentheses you just say what you want to know what the string length is of so in this case we want to know it for string just our string variable and then let's go ahead and echo out string so save that pop over here and as you can see it's 15 so it's literally 15 characters so those are just a few different uh ways you can work with strings as always thank you for watching and stay tuned in the next video we'll be talking about even more ways and things that we can do with strings
Original Description
Link to the full playlist:
http://www.youtube.com/playlist?list=PLQVvvaa0QuDcYpcjrNB43_iKqla5UeQw_
Sentdex.com
Facebook.com/sentdex
Twitter.com/sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 34 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
▶
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
Related Reads
📰
📰
📰
📰
The Impact of AI on Learning: A High School Student’s Perspective
Medium · AI
Prompt Engineering in 2026: The Essential Skill for Working with AI
Medium · AI
I Cut 1,300 Lines of CLAUDE.md — My Token Bill Dropped 75%
Medium · LLM
Fine-Tuning Embedding Models for Domain-Specific RAG
Medium · RAG
🎓
Tutor Explanation
DeepCamp AI